locked
Add XML element to existing XmlNodeList C#? RRS feed

  • Question

  • User968821410 posted

    Hi,

    I am getting strucked in below concept.

    I have an xml string  like this..

    <ResponseData> 
      <a> 
        <a1>1</a1> 
        <a2>1</a2> 
      </a> 
      <b> 
        <b1>1</b1> 
        <b2>2</b2> 
      </b> 
      <b> 
        <b1>3</b1> 
        <b2>4</b2> 
      </b> 
      <c> 
        <c1>1</c1> 
        <c2>2</c2> 
      </c> 
    </ResponseData> 

    and desired output of XML string is

    <ResponseData> 
      <a> 
        <a1>1</a1> 
        <a2>1</a2> 
      </a> 
      <B>
        <b> 
          <b1>1</b1> 
          <b2>2</b2> 
        </b> 
        <b> 
          <b1>3</b1> 
          <b2>4</b2> 
        </b> 
      </B>
      <c> 
        <c1>1</c1> 
        <c2>2</c2> 
      </c> 
    </ResponseData> 

    And I have tired until this part and could not find a solution how to add new element to existing XMLNodeList

     XmlDocument doc = new XmlDocument();
                doc.LoadXml("<ResponseData><a><a1>1</a1><a2>1</a2></a><b><b1>1</b1><b2>2</b2></b><b><b1>3</b1><b2>4</b2></b><c><c1>1</c1><c2>2</c2></c></ResponseData>");
    
                XmlNode root = doc.DocumentElement;
    
                //Create a new node.
                XmlElement elem = doc.CreateElement("B");
    
                XmlNodeList accountNodes = doc.GetElementsByTagName("b");         
             
                // ?
              

    How to add <B></B> element for whole <b></b> elementsList?

    Please help me on this.

    Thanks in advance



    Tuesday, April 4, 2017 3:53 AM

Answers

  • User-1509636757 posted

    Going thru XElement will be much easier than XmlDocument:

    using System;
    using System.Xml.Linq;
    using System.Xml.XPath;
    
    namespace WebApp
    {
        public partial class WebForm89 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                XElement xSrc = XElement.Parse("<ResponseData><a><a1>1</a1><a2>1</a2></a><b><b1>1</b1><b2>2</b2></b><b><b1>3</b1><b2>4</b2></b><c><c1>1</c1><c2>2</c2></c></ResponseData>");
                XElement xResult = new XElement("ResponseData",
                                                    xSrc.XPathSelectElements("a"),
                                                    new XElement("B", xSrc.XPathSelectElements("b")),
                                                    xSrc.XPathSelectElements("c"));
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 4, 2017 7:28 AM
  • User-1838255255 posted

    Hi Narsa8,

    According to your description, as far as I know, you could use remove method to remove the related elements. Then append a new element. Then append removed elements into new element. Please refer to this sample:

    Sample Code:

    protected void Button1_Click(object sender, EventArgs e)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(@"<NonFuel><Desc>Fuel</Desc><Desc>Non</Desc><Description></Description><Dispute>0</Dispute></NonFuel>");
                XmlNode root = xmlDoc.DocumentElement;
    
                //Remove the title element.
                root.RemoveChild(root.ChildNodes[0]);
                root.RemoveChild(root.ChildNodes[0]);
    
    
                XmlElement elem = xmlDoc.CreateElement("title");
              
                root.AppendChild(elem);
                XmlNode Descone = xmlDoc.CreateElement("Desc");
                Descone.InnerText = "Fuel";
                XmlNode Desctwo = xmlDoc.CreateElement("Desc");
                Desctwo.InnerText = "Non";
    
                root.ChildNodes[2].AppendChild(Descone);
                root.ChildNodes[2].AppendChild(Desctwo);
            }

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 4, 2017 8:09 AM

All replies

  • User-1509636757 posted

    Going thru XElement will be much easier than XmlDocument:

    using System;
    using System.Xml.Linq;
    using System.Xml.XPath;
    
    namespace WebApp
    {
        public partial class WebForm89 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                XElement xSrc = XElement.Parse("<ResponseData><a><a1>1</a1><a2>1</a2></a><b><b1>1</b1><b2>2</b2></b><b><b1>3</b1><b2>4</b2></b><c><c1>1</c1><c2>2</c2></c></ResponseData>");
                XElement xResult = new XElement("ResponseData",
                                                    xSrc.XPathSelectElements("a"),
                                                    new XElement("B", xSrc.XPathSelectElements("b")),
                                                    xSrc.XPathSelectElements("c"));
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 4, 2017 7:28 AM
  • User-1838255255 posted

    Hi Narsa8,

    According to your description, as far as I know, you could use remove method to remove the related elements. Then append a new element. Then append removed elements into new element. Please refer to this sample:

    Sample Code:

    protected void Button1_Click(object sender, EventArgs e)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(@"<NonFuel><Desc>Fuel</Desc><Desc>Non</Desc><Description></Description><Dispute>0</Dispute></NonFuel>");
                XmlNode root = xmlDoc.DocumentElement;
    
                //Remove the title element.
                root.RemoveChild(root.ChildNodes[0]);
                root.RemoveChild(root.ChildNodes[0]);
    
    
                XmlElement elem = xmlDoc.CreateElement("title");
              
                root.AppendChild(elem);
                XmlNode Descone = xmlDoc.CreateElement("Desc");
                Descone.InnerText = "Fuel";
                XmlNode Desctwo = xmlDoc.CreateElement("Desc");
                Desctwo.InnerText = "Non";
    
                root.ChildNodes[2].AppendChild(Descone);
                root.ChildNodes[2].AppendChild(Desctwo);
            }

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 4, 2017 8:09 AM