1

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.

3
  • 3
    The result you describe wouldn't be valid Xml - you need one element as the root of your doc. My guess: Could it be that your problem is rather handling the namespace prefix ns0?
    – Filburt
    Jun 18, 2019 at 6:38
  • @Filburt I dont need it to be a Valid XML, I just need to output to be the result I want.
    – Wookoai
    Jun 18, 2019 at 6:56
  • Well you can't represent malformed XML using an XDocument. Why not just return a List<XElement> instead, then serialize each one individually?
    – dbc
    Jun 18, 2019 at 10:04

2 Answers 2

3

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();
1
  • Thanks for the throw in the right direction I got i solved with your input. Thansk again mate.
    – Wookoai
    Jun 19, 2019 at 8:13
0

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;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.