GeneBe
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</FirstNam e>
<AltKey>000011101</AltKey>
<HCFANbr>000011101</HCFANb r>
<ContractNbr>000011101</Co ntractNbr>
<YmdBirth>2001-06-14T00:00 :00</YmdBi rth>
</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</FirstNam e>
<AltKey>000011101</AltKey>
<HCFANbr>000011101</HCFANb r>
<ContractNbr>000011101</Co ntractNbr>
<YmdBirth>2001-06-14T00:00 :00</YmdBi rth>
</Member>
<?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</FirstNam
<AltKey>000011101</AltKey>
<HCFANbr>000011101</HCFANb
<ContractNbr>000011101</Co
<YmdBirth>2001-06-14T00:00
</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</FirstNam
<AltKey>000011101</AltKey>
<HCFANbr>000011101</HCFANb
<ContractNbr>000011101</Co
<YmdBirth>2001-06-14T00:00
</Member>
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</FirstNam e>
<AltKey>000011101</AltKey>
<HCFANbr>000011101</HCFANb r>
<ContractNbr>000011101</Co ntractNbr>
<YmdBirth>2001-06-14T00:00 :00</YmdBi rth>
</Member>
<?xml version="1.0" encoding="utf-8"?>
<Member xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<LastName>Bond</LastName>
<FirstName>James</FirstNam
<AltKey>000011101</AltKey>
<HCFANbr>000011101</HCFANb
<ContractNbr>000011101</Co
<YmdBirth>2001-06-14T00:00
</Member>
Hi GeneBe;
I believe this will do what you need. Let me know.
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");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you so much!
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).