0



I am trying to replace a node in my XML file, but I need some help resolving how to replace a Node with a non-unique name or identifier.

The Question is, How do I replace to following Node with Data from XmlDataReplace:

/Document/ExclusionContainer/Mobile_Devices

Below is some sample code and data used in the sample code.

Sample Code

private void ReplaceXmlArray()
{
 System.Xml.XmlDocument OriginalXmlDoc = new System.Xml.XmlDocument();
 //Scroll Down for XmlData     
 OriginalXmlDoc.LoadXml("StackOverflow - XmlDataOriginal");      
 string NewXmlContent = "StackOverFlow - XmlDataReplace";

 //Need help here on Code to Replace
 //Node in OriginalXmlDoc
}

XmlDataOriginal - Original Doc

<Document>

 <DefaultContainer>

  <Mobile_Devices>
   <Mobile_Device>
    <Id>1</Id>
    <Name>Device-One</Name>
   </Mobile_Device>
  </Mobile_Devices>

 </DefaultContainer> 

 <ExlusionContainer>

  <Mobile_Devices>
   <Mobile_Device>
    <Id>2</Id>
    <Name>Device-Two</Name>
   </Mobile_Device>
  </Mobile_Devices>

  <Laptops />

 </ExclusionContainer>

</Document>

XmlDataReplace - Will Replace Data in XmlData1 (Document/ExclusionContainer/)

<Mobile_Devices>
   <Mobile_Device>
    <Id>2</Id>
    <Name>Device-Two</Name>
   </Mobile_Device>
   <Mobile_Device>
    <Id>3</Id>
    <Name>Device-Three</Name>
   </Mobile_Device>
</Mobile_Devices>
4
  • It's very unclear what you're asking, to be honest - you haven't shown any code to even try doing a replacement, nor is it clear in what way /Document/ExclusionContainer/Mobile_Devices identifies more than one node... Commented Sep 11, 2015 at 10:36
  • The question is "How do I replace /Document/ExclusionContainer/Mobile_Devices with the data from 'XmlDataReplace' ". I tried to do it but I am having problems collecting the right Node Commented Sep 11, 2015 at 10:40
  • 1
    So what have you tried in terms of finding the right node? It seems that the replacement part is pretty irrelevant at the moment - your question is really about finding it. Personally I'd strongly recommend using LINQ to XML rather than XmlDocument - that will make it even easier than it already is with XmlDocument. Commented Sep 11, 2015 at 10:41
  • Yes, I agree it might be more about finding it. So far the method OriginalXmlDoc.GetElementsByTagName keeps giving me exceptions but I will look into the Linq to Xml. thanks Commented Sep 11, 2015 at 10:45

2 Answers 2

1

Got your point. Correct me if i am wrong :- You want to update(replace or create) the data. Lets do this way I choose these two xmls

Orignal.xml stored in c:\data\orignal.xml

<?xml version="1.0" encoding="utf-8"?>
<Document>
  <DefaultContainer>
    <Mobile_Devices>
      <Mobile_Device>
        <Id>1</Id>
        <Name>Device-One</Name>
      </Mobile_Device>
    </Mobile_Devices>
  </DefaultContainer>
  <ExlusionContainer>
    <Mobile_Devices>
      <Mobile_Device>
        <Id>2</Id>
        <Name>Device-Two</Name>
      </Mobile_Device>
    </Mobile_Devices>
  </ExlusionContainer>
</Document>

the second replace.xml stored in c:\data\replace.xml

<?xml version="1.0" encoding="utf-8" ?>
<ExclusionContainer>
  <Mobile_Devices>
    <Mobile_Device>
      <Id>2</Id>
      <Name>Device-Two</Name>
    </Mobile_Device>
  </Mobile_Devices>
  <Mobile_Devices>
    <Mobile_Device>
      <Id>3</Id>
      <Name>Device-Three</Name>
    </Mobile_Device>
  </Mobile_Devices>
  <Laptops />
  </ExclusionContainer>

Copy paste the below code

     XmlDocument originalDoc = new XmlDocument();
        originalDoc.Load("c:\\data\\Orignal.xml");
        XmlNode exclusionNode = originalDoc.SelectSingleNode("/Document/ExlusionContainer");

        XmlDocument docToReplace = new XmlDocument();
        docToReplace.Load("c:\\data\\replace.xml");

        XmlNode replaceNode = docToReplace.SelectSingleNode("/ExclusionContainer");
        exclusionNode.InnerXml = replaceNode.InnerXml;
        originalDoc.Save("c:\\data\\orignal.xml");
Sign up to request clarification or add additional context in comments.

1 Comment

This indeed does the job. Thanks for the help
0

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = 

                "<Document>" +
                  "<DefaultContainer>" +
                    "<Mobile_Devices>" +
                      "<Mobile_Device>" +
                        "<Id>1</Id>" +
                        "<Name>Device-One</Name>" +
                      "</Mobile_Device>" +
                    "</Mobile_Devices>" +
                  "</DefaultContainer>" +
                  "<ExlusionContainer>" +
                    "<Mobile_Devices>" +
                      "<Mobile_Device>" +
                        "<Id>2</Id>" +
                        "<Name>Device-Two</Name>" +
                      "</Mobile_Device>" +
                    "</Mobile_Devices>" +
                    "<Laptops />" +
                  "</ExlusionContainer>" +
                "</Document>";

            string newNode = 
                "<Mobile_Device>" +
                    "<Id>3</Id>" +
                    "<Name>Device-Three</Name>" +
                   "</Mobile_Device>";



            XDocument doc = XDocument.Parse(input);
            XElement newElement = XElement.Parse(newNode);

            XElement exlusionContainer = doc.Descendants("ExlusionContainer").FirstOrDefault();
            XElement mobileDevice = exlusionContainer.Element("Mobile_Devices").Element("Mobile_Device");
            mobileDevice.ReplaceWith(new XElement[] { mobileDevice, newElement });
        }
    }
}
​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.