2012-07-26 106 views
0

我加入XML文件在我的Windows应用程序,我想添加值从文本框.. 我用下面的代码,如何将xml数据插入到c#中现有的xml中?

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(子根)错误;任何人都可以帮助我,我犯了错误。

+0

有什么错误呢? – 2012-07-26 07:03:06

+0

对象引用没有设置为一个实例... – chitra 2012-07-26 07:09:47

+0

LINQ to XML在这种情况下真的很不错:) 你得到这个错误,因为没有'Subroot'的实例尝试添加这一行:'Subroot sub = new Subroot ();'在这行之前''Subroot.AppendChild(Companycode); '并将所有'Subroot'改为'sub' – harry180 2012-07-26 07:53:30

回答

1

root为空。尝试在创建XML文件时添加Root元素。

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 [email protected]"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); 
+0

嗨,我用过,它返回保存,但没有数据在xml文件中 – chitra 2012-07-26 08:14:23

+1

您可以设置物理路径'c:\ file.xml'或查看'Debug /斌文件夹,如果你的应用程序是winform或控制台。 – adatapost 2012-07-26 08:15:50

+0

在窗体中。 – chitra 2012-07-26 08:21:54

0

In null XML DocumentElement is null。尝试添加Subroot到文件:

 
XmlElement root = doc.DocumentElement;

root.AppendChild(Subroot); doc.AppendChild(root);

// new code 
doc.AppendChild(Subroot); 
+0

我试过了我的想法,它显示了msg已保存,但是在xml文件中...没有数据只存在于<?xml version =“1.0”encoding =“utf -8“?>我通过添加新项目添加了我的xml文件 – chitra 2012-07-26 07:12:18

+0

@chitra:我正确测试了我的代码及其工作。检查输出xml。 – Ria 2012-07-26 07:14:50

+0

我应该在xml中添加任何元素,然后保存btn – chitra 2012-07-26 08:15:09

2

您可以使用LINQ到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"); 
相关问题