0

Windowsアプリケーションに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(Subroot); の近くでエラーが表示されます。誰でも私を助けることができますか、私は間違いを犯しました。

4

3 に答える 3

2

LinqtoXMLを使用できます。例を次に示します。

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");
于 2012-07-26T07:11:26.523 に答える
1

rootヌルです。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);
于 2012-07-26T07:09:28.263 に答える
0

null の XMLDocumentElementnull. Subrootドキュメントに追加してみてください:

XmlElement ルート = doc.DocumentElement;

root.AppendChild(サブルート); doc.AppendChild(ルート);

// new code
doc.AppendChild(Subroot);
于 2012-07-26T07:08:34.667 に答える