locked
Insert a Document Fragment into a Node RRS feed

  • Question

  • User-1853979378 posted

    NOTE: Sorry about the formatting - I corrected it below.


    I've seen several threads in the forum that were similar to this problem, but none where I was able to glean specifically what I'm trying to do.

    I am attempting insert data into an XML document at a specific point.

    Here is an example XML document


    <?xml version="1.0" encoding="utf-8" ?>
    <Month>
      <Day id="6">
        <Number id="1">
          <Comment>Some text</Comment>
          <Comment>Some text</Comment>
        </Number>
      </Day>
      <Day id="15">
        <Number id="1">
          <Comment>Some text</Comment>
          <Comment>Some text</Comment>
        </Number>
        <Number id="2">
          <Comment>Some text</Comment>
        </Number>
      </Day>
      <Day id="18">
        <Number id="1">
          <Comment>Some text</Comment>
        </Number>
      </Day>
    </Month>


    Now, suppose I want to insert another <Comment> in the <Day id=15><Number id=2> node. How do I do that?

    Thursday, July 23, 2009 8:39 PM

Answers

  • User-1293249277 posted

    U can do it as following.. I have stored ur xml string in the XmlFIle9 here...

    string fileName = Server.MapPath("XMLFile9.xml");
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(fileName);

            XmlNode xComment = xDoc.CreateNode(XmlNodeType.Element, "Comment", "");
            xComment.InnerText = "This is the new comment";

            XmlNode xNodeSelect = xDoc.SelectSingleNode("Month/Day[@id = 15]/Number[@id = 2]");
            xNodeSelect.AppendChild(xComment);

            string seeString = xDoc.DocumentElement.OuterXml;


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 24, 2009 1:19 AM

All replies

  • User-1293249277 posted

    U can do it as following.. I have stored ur xml string in the XmlFIle9 here...

    string fileName = Server.MapPath("XMLFile9.xml");
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(fileName);

            XmlNode xComment = xDoc.CreateNode(XmlNodeType.Element, "Comment", "");
            xComment.InnerText = "This is the new comment";

            XmlNode xNodeSelect = xDoc.SelectSingleNode("Month/Day[@id = 15]/Number[@id = 2]");
            xNodeSelect.AppendChild(xComment);

            string seeString = xDoc.DocumentElement.OuterXml;


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 24, 2009 1:19 AM
  • User-1853979378 posted

    Kavita,

    You the man!  Er... the woman!

    Thanks!Smile

    Friday, July 24, 2009 2:25 AM