Wookoai
Wookoai

Reputation: 47

remove root node but keep all child nodes

I have this xml that is parsed through

<ns0:Root xmlns:ns0="http://Core.Schemas.SouCurrencyRate">
    <Test>
        <CurrencyCode>SEKAUDPMI</CurrencyCode>
        <CurrencyType>AUD</CurrencyType>
        <CurrencyDate>2019 Juni</CurrencyDate>
        <CurrencyRate>6.5656</CurrencyRate>
        <RecordCreated>2019-06-30</RecordCreated>
        <RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
    </Test>
    <Test>
        <CurrencyCode>SEKBRLPMI</CurrencyCode>
        <CurrencyType>BRL</CurrencyType>
        <CurrencyDate>2019 Juni</CurrencyDate>
        <CurrencyRate>2.4376</CurrencyRate>
        <RecordCreated>2019-06-30</RecordCreated>
        <RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
    </Test>
    <Test>
        <CurrencyCode>SEKCADPMI</CurrencyCode>
        <CurrencyType>CAD</CurrencyType>
        <CurrencyDate>2019 Juni</CurrencyDate>
        <CurrencyRate>7.0771</CurrencyRate>
        <RecordCreated>2019-06-30</RecordCreated>
        <RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
    </Test>
    <Test>
        <CurrencyCode>SEKCHFPMI</CurrencyCode>
        <CurrencyType>CHF</CurrencyType>
        <CurrencyDate>2019 Juni</CurrencyDate>
        <CurrencyRate>951.7346</CurrencyRate>
        <RecordCreated>2019-06-30</RecordCreated>
        <RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
    </Test>
</ns0:Root>

The xml has ("Test") child nodes that occure a lot of time. What I need to do is actually remove (" <ns0:Root xmlns:ns0="http://Core.Schemas.SouCurrencyRate"> ")

So that the XML output is

<Test>
    <CurrencyCode>SEKAUDPMI</CurrencyCode>
    <CurrencyType>AUD</CurrencyType>
    <CurrencyDate>2019 Juni</CurrencyDate>
    <CurrencyRate>6.5656</CurrencyRate>
    <RecordCreated>2019-06-30</RecordCreated>
    <RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
</Test>
<Test>
    <CurrencyCode>SEKBRLPMI</CurrencyCode>
    <CurrencyType>BRL</CurrencyType>
    <CurrencyDate>2019 Juni</CurrencyDate>
    <CurrencyRate>2.4376</CurrencyRate>
    <RecordCreated>2019-06-30</RecordCreated>
    <RecordCreatedSOP>2019-06-01</RecordCreatedSOP>
</Test>

What I've tried to do is

    System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
    xDoc.Load(bizobj.Message.BodyPart.GetOriginalDataStream());


    System.Xml.XmlNode xNode = xDoc.FirstChild;
    System.Xml.XmlNode xParent = xNode.ParentNode;
    System.Xml.XmlNodeList xChilds = xNode.ChildNodes;
    xDoc.RemoveChild(xNode);
    foreach (System.Xml.XmlNode node in xChilds)
    {
        xParent.AppendChild(node);
    }

But the main problem is that the returned xml only contains one child ("Test") and not the others.

I have also tried using Linq where I found this on a post

XDocument input = XDocument.Load("input.xml");
XElement firstChild = input.Root.Elements().First();

But this returns the same thing and does not help my case.

Can someone help me in the right direction?

Thanks.

Upvotes: 1

Views: 1533

Answers (2)

Wookoai
Wookoai

Reputation: 47

Just putting my final code here if anyone wants to take a look and use it. Filburt was the one that threw me in the right direction anyway so look as his aswell.

 System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
        xDoc.Load(bizobj.Message.BodyPart.GetOriginalDataStream());
        string output = xDoc.DocumentElement.InnerXml;
    byte[] byteArray = Encoding.ASCII.GetBytes(output);
    MemoryStream stream = new MemoryStream(byteArray);

    stream.Position = 0;
    bizobj.Message.BodyPart.Data = stream;


    return bizobj;

Upvotes: 0

Filburt
Filburt

Reputation: 18061

If you really only need the text string, the fastest way to achieve this is using an XmlReader on the Root XElement:

XDocument xDoc = XDocument.Parse(bizobj.Message.BodyPart.GetOriginalDataStream());
XElement root = xDoc.Root;
XmlReader reader = root.CreateReader();
reader.MoveToContent();
string txt = reader.ReadInnerXml();

Upvotes: 3

Related Questions