Link to home
Start Free TrialLog in
Avatar of GeneBe
GeneBeFlag for United States of America

asked on

i have an extra root node in my xml objet that i want to remove using c#

using C# remove the root node in an xml
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.w3.org/2001/XMLSchema-instance"><Member xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <LastName>Bond</LastName>
  <FirstName>James</FirstName>
  <AltKey>000011101</AltKey>
  <HCFANbr>000011101</HCFANbr>
  <ContractNbr>000011101</ContractNbr>
  <YmdBirth>2001-06-14T00:00:00</YmdBirth>
</Member></string>      

I want to remove
<string xmlns="http://www.w3.org/2001/XMLSchema-instance">
and
</string>      
so it will look like this:

<?xml version="1.0" encoding="utf-8"?>
<Member xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <LastName>Bond</LastName>
  <FirstName>James</FirstName>
  <AltKey>000011101</AltKey>
  <HCFANbr>000011101</HCFANbr>
  <ContractNbr>000011101</ContractNbr>
  <YmdBirth>2001-06-14T00:00:00</YmdBirth>
</Member>
Avatar of kaufmed
kaufmed
Flag of United States of America image

You can achieve this by loading the document into an XmlDocument and skipping down to the node you care about. For example:

class Program
{
    static void Main(string[] args)
    {
        string xml = GetXml();
        XmlDocument xdoc = new XmlDocument();
        XmlNode xmlDeclaration;
        XmlNode node;

        xdoc.LoadXml(xml);
        xmlDeclaration = xdoc.FirstChild;
        node = xmlDeclaration.NextSibling.FirstChild;

        xdoc.RemoveAll();
        xdoc.AppendChild(xmlDeclaration);
        xdoc.AppendChild(node);

        Console.OutputEncoding = Encoding.GetEncoding(((XmlDeclaration)xmlDeclaration).Encoding);

        xdoc.Save(Console.Out);

        Console.ReadKey();
    }

    private static string GetXml()
    {
        return @"<?xml version=""1.0"" encoding=""utf-8""?>
<string xmlns=""http://www.w3.org/2001/XMLSchema-instance""><Member xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
<LastName>Bond</LastName>
<FirstName>James</FirstName>
<AltKey>000011101</AltKey>
<HCFANbr>000011101</HCFANbr>
<ContractNbr>000011101</ContractNbr>
<YmdBirth>2001-06-14T00:00:00</YmdBirth>
</Member></string>";
    }
}

Open in new window


In the above, we grab the XML declaration so that we can output it as well. Then we navigate down the hierarchy by looking for the XML declaration's first sibling (i.e. the root node), and then looking for that node's first child (i.e. the node you care about). Then we remove all nodes from the XmlDocument. Then we append back the XML declaration and the node that you care about. Then we output the resulting XML (in this example, we output to the Console).
Avatar of GeneBe

ASKER

The output still has the <string> tag. I want to replace that with the Member tag and take out the old member tag so it will look like this:
<?xml version="1.0" encoding="utf-8"?>
<Member xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <LastName>Bond</LastName>
  <FirstName>James</FirstName>
  <AltKey>000011101</AltKey>
  <HCFANbr>000011101</HCFANbr>
  <ContractNbr>000011101</ContractNbr>
  <YmdBirth>2001-06-14T00:00:00</YmdBirth>
</Member>
Hi GeneBe;

I believe this will do what you need. Let me know.
using System.Linq;
using System.Xml.Linq;


// Change file name and directory to where your file is.
XElement doc = XElement.Load("test.xml");

IEnumerable<XElement> childList =
    from elements in doc.Elements()
    select elements;

XDocument newDoc = new XDocument(new XDeclaration("1.0", "utf-8", "true"));

foreach (XElement rootNode in childList)
{
    rootNode.Name = rootNode.Name.LocalName;
    foreach (XElement child in rootNode.Elements())
    {
        child.Name = child.Name.LocalName;
    }
    newDoc.Add(rootNode);
}

// Save the new document to the file system
newDoc.Save("TestNew.xml");

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GeneBe

ASKER

thank you so much!