Append XML string block to existing XmlDocument

前端 未结 6 1466
礼貌的吻别
礼貌的吻别 2020-12-14 15:29

I have an XmlDocument that already exists and is read from a file.

I would like to add a chunk of Xml to a node in the document. Is there a good way to create and a

相关标签:
6条回答
  • 2020-12-14 15:57

    Try this:

    employeeNode.InnerXml = "<Demographic><Age/><DOB/></Demographic>";
    

    Alternatively (if you have another XML document that you want to use):

    employeeNode.AppendChild(employeeNode.OwnerDocument.ImportNode(otherXmlDocument.DocumentElement, true));
    
    0 讨论(0)
  • 2020-12-14 16:00

    Consider using an XmlWriter for building your fragments on a StringBuilder as this will provide validation and character substitution for you.

    0 讨论(0)
  • 2020-12-14 16:01

    I suggest using XmlDocument.CreateDocumentFragment if you have the data in free form strings. You'll still have to use AppendChild to add the fragment to a node, but you have the freedom of building the XML in your StringBuilder.

    XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(@"<MyXml><Employee></Employee></MyXml>");
    
    XmlDocumentFragment xfrag = xdoc.CreateDocumentFragment();
    xfrag.InnerXml = @"<Demographic><Age/><DOB/></Demographic>";
    
    xdoc.DocumentElement.FirstChild.AppendChild(xfrag);
    
    0 讨论(0)
  • 2020-12-14 16:07

    As an alternative, this is how you could do it in a more LINQy 3.5 manner:

     XDocument doc = XDocument.Load(@"c:\temp\test.xml");
     XElement demoNode = new XElement("Demographic");
     demoNode.Add(new XElement("Age"));
     demoNode.Add(new XElement("DOB"));
     doc.Descendants("Employee").Single().Add(demoNode);
     doc.Save(@"c:\temp\test2.xml");
    
    0 讨论(0)
  • 2020-12-14 16:08

    None of this was working for me so i played around abit and here is my solution.

    First load up a text field(you can put it to visible = false in public version) load the data in to the text field like so.

    string Path = Directory.GetCurrentDirectory() + "/2016";
                string pathFile = Path + "/klanten.xml";
                StreamReader sr = new StreamReader(pathFile);
                txt.Text = sr.ReadToEnd();
                sr.Close();
    

    on the save button load up the text field en save it. Dont forget u will have to refresh the text field after that, if u want to add multiple addresses/names, i have not included that part.

    string name = Globals.s_Name;
            string klanten = txt.Text;
            string s = klanten;
            XmlDocument xdoc = new XmlDocument();
    
            string klant = "<voornaam>" + naamBox1.Text + "</voornaam>";
            xdoc.LoadXml(s);
            XmlDocumentFragment xfrag = xdoc.CreateDocumentFragment();
            xfrag.InnerXml = klant;
            xdoc.DocumentElement.FirstChild.AppendChild(xfrag);
            xdoc.Save(name + "/klanten.xml");
    
    0 讨论(0)
  • 2020-12-14 16:09

    All that I do is creating a new dataset object and open the xml file using ReadXML myDataset.ReadXML(path and file name).

    Then add or remove the rows that I need and save the document again using myDataset.WriteXML(path and file name).

    Bye.

    0 讨论(0)
提交回复
热议问题