如何将xml数据插入到c#中现有的xml中?

如何将xml数据插入到c#中现有的xml中?,c#,.net,xml,C#,.net,Xml,我在windows应用程序中添加了xml文件,我想从textbox向该文件添加值。。 我使用了以下代码 string path = "codedata.xml"; XmlDocument doc = new XmlDocument(); if (!System.IO.File.Exists(path)) { //Create neccessary nodes XmlDeclaration declar

我在windows应用程序中添加了xml文件,我想从textbox向该文件添加值。。 我使用了以下代码

string path = "codedata.xml";
        XmlDocument doc = new XmlDocument();
        if (!System.IO.File.Exists(path))
        {
            //Create neccessary nodes
            XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            XmlComment comment = doc.CreateComment("This is an XML Generated File");
            doc.AppendChild(declaration);
            doc.AppendChild(comment);
        }
        else //If there is already a file
        {
            //    //Load the XML File
            doc.Load(path);
        }

        //Get the root element
        XmlElement root = doc.DocumentElement;

        XmlElement Subroot = doc.CreateElement("data");
        XmlElement Companycode = doc.CreateElement("Companycode");
        XmlElement Productcode = doc.CreateElement("Productcode");
        XmlElement Productname = doc.CreateElement("Productname");
        XmlElement Brandcode = doc.CreateElement("Brandcode");
        XmlElement Brandname = doc.CreateElement("Brandname");

        Companycode.InnerText = txt_companycode.Text;
        Productcode.InnerText = txt_productcode.Text;
        Productname.InnerText = txt_productname.Text;
        Brandcode.InnerText = txt_brandcode.Text;
        Brandname.InnerText = txt_brandname.Text;

        Subroot.AppendChild(Companycode);
        Subroot.AppendChild(Productcode);
        Subroot.AppendChild(Productname);
        Subroot.AppendChild(Brandcode);
        Subroot.AppendChild(Brandname);
        root.AppendChild(Subroot);
        doc.AppendChild(root);


        //Save the document
        doc.Save(path);


        //Show confirmation message
        MessageBox.Show("Details  added Successfully");

它在root.AppendChild(Subroot)附近显示错误;谁能帮我一个忙,是我弄错了。

在null XML
DocumentElement
中是
null
。尝试将
Subroot
添加到文档:


在null XML
DocumentElement
中为
null
。尝试将
Subroot
添加到文档:


根目录
为空。创建XML文件时尝试添加根元素

 if (!System.IO.File.Exists(path))
        {
            //Create neccessary nodes
            XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            XmlComment comment = doc.CreateComment("This is an XML Generated File");
            doc.AppendChild(declaration);
            doc.AppendChild(comment);
            doc.AppendChild(doc.CreateElement("Root"));
        }
或者使用LINQ-XML

string _file=@"c:\sample.xml";
XDocument doc;

if (!File.Exists(_file))
{
    doc = new XDocument();
    doc.Add(new XElement("Root"));
}
else
{
    doc = XDocument.Load(_file);
}

doc.Root.Add(
      new XElement("data",
                   new XElement("CompanyCode","C101"),
                   new XElement("ProductCode","P101")
            ) 
      );
doc.Save(_file);

根目录
为空。创建XML文件时尝试添加根元素

 if (!System.IO.File.Exists(path))
        {
            //Create neccessary nodes
            XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            XmlComment comment = doc.CreateComment("This is an XML Generated File");
            doc.AppendChild(declaration);
            doc.AppendChild(comment);
            doc.AppendChild(doc.CreateElement("Root"));
        }
或者使用LINQ-XML

string _file=@"c:\sample.xml";
XDocument doc;

if (!File.Exists(_file))
{
    doc = new XDocument();
    doc.Add(new XElement("Root"));
}
else
{
    doc = XDocument.Load(_file);
}

doc.Root.Add(
      new XElement("data",
                   new XElement("CompanyCode","C101"),
                   new XElement("ProductCode","P101")
            ) 
      );
doc.Save(_file);

您可以使用Linq to XML,下面是一个示例:

var xDoc = XElement.Load("FilePath");
if (xDoc == null)
   return;

var myNewElement = new XElement("ElementName"
   new XAttribute("AttributeName", value1),
   new XAttribute("AttributeName", value2)
   //And so on ...
);
xDoc.Add(myNewElement);
xDoc.Save("FilePath");

您可以使用Linq to XML,下面是一个示例:

var xDoc = XElement.Load("FilePath");
if (xDoc == null)
   return;

var myNewElement = new XElement("ElementName"
   new XAttribute("AttributeName", value1),
   new XAttribute("AttributeName", value2)
   //And so on ...
);
xDoc.Add(myNewElement);
xDoc.Save("FilePath");


对象引用未设置为实例…LINQ to XML在这种情况下非常好:)您会遇到此错误,因为没有
Subroot
的实例,请尝试添加此行:
Subroot sub=new Subroot()在这一行之前
Subroot.AppendChild(公司代码)
并将所有
Subroot
更改为
sub
对象引用未设置为实例…LINQ to XML在这种情况下非常好:)您会收到此错误,因为没有
Subroot
的实例,请尝试添加此行:
Subroot sub=new Subroot()在这一行之前
Subroot.AppendChild(公司代码)
并将所有
Subroot
更改为
sub
我尝试了你的想法,它显示了已保存的消息,但在xml文件中…没有数据是空的,只有我通过add new添加了我的xml文件item@chitra:我测试了我的代码并正确运行。检查输出xml。我是否应该在xml中添加任何元素,在保存btni尝试您的想法之前,它显示已保存的消息,但在xml文件中…没有数据是空的,只有我通过add new添加了我的xml文件item@chitra:我测试了我的代码并正确运行。检查输出xml。如果我在xml中添加任何元素,在保存btnhi之前,我使用了,它会返回已保存但xml文件中没有数据。当我使用断点进行测试时,您要么设置了物理路径
c:\file.xml
,要么查看
Debug/Bin
文件夹,如果您的应用程序是winform或console.hai,它显示了包含我保存的数据的xml文件。但是如果我在codedata.xml中看到,它是空的。它保存在哪里?尝试设置绝对路径,看看会发生什么?嗨,我用过,它返回保存的,但xml文件中没有数据。当我使用断点进行测试时,你要么设置了物理路径
c:\file.xml
,要么查看
Debug/Bin
文件夹,如果你的应用程序是winform或console.hai,它显示了包含我保存的数据的xml文件。但是如果我在codedata.xml中看到,它是空的。在哪里保存?尝试设置绝对路径,看看会发生什么?