0

I have xml and I want to get the value of node . My XML looks:

<?xml version="1.0" encoding="UTF-8"?>
<jdf:root xmlns:jdf="xxxxxxxx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<jdf:header>

    <jdf:locale-code>xx</jdf:locale-code>
    <jdf:country-code>xx</jdf:country-code>
</jdf:header>
<app:data xmlns:app="xxxxxx">
    <app:EventOut xmlns:ns2="xxxxxxx">
        <app:eventId>xxx</app:eventId>
        <app:distributorId>xxx</app:distributorId>
        <app:distributionNetworkId>xxx</app:distributionNetworkId>
        <app:typology>xxx</app:typology>
        <app:targets>
            <app:target>
                ......
            </app:target>
            <app:target>
                .....
            </app:target>
        </app:targets>
        <app:object>
            <ns2:internalEventObject>
                <ns2:id>!!!!!!!!</ns2:id>
                <ns2:lang1>xxx</ns2:lang1>
            </ns2:internalEventObject>
        </app:object>
        ...
    </app:EventOut>
</app:data>

I just try:

    XmlDocument xml = new XmlDocument();
    xml.LoadXml(eventOutXml);

    var nsmgr = new XmlNamespaceManager(xml.NameTable);
    nsmgr.AddNamespace("ns2", "http://www.w3.org/1999/XSL/Transform");

    XmlNode anode = xml.SelectSingleNode("//ns2:id", nsmgr);

But it is not working.

In My XML I have few namespaces:jdf, app, ns2. Maybe I must add all these?

9
  • 1
    " it is not working." How? Commented May 5, 2016 at 12:45
  • 1
    I may be wrong, but I think that the ns2: namespace prefix is not required in SelectSingleNode. Commented May 5, 2016 at 12:48
  • Possible duplicate of Getting specified Node values from XML document Commented May 5, 2016 at 12:57
  • 3
    Can you make sure the ns2 namespace value is as same as what you used in your c# code? Commented May 5, 2016 at 13:02
  • 2
    @krolik1991 are you sure ns2 in the actual XML declared as such : xmlns:ns2="http://www.w3.org/1999/XSL/Transform" ? Commented May 5, 2016 at 13:04

2 Answers 2

1

Your xml was missing end tag . And the namespace you added in the code was different in the xml. I did those two changes to the xml and was able to get this working.

Updated xml:

<?xml version="1.0" encoding="UTF-8"?>
<jdf:root xmlns:jdf="xxxxxxxx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<jdf:header>
    <jdf:locale-code>xx</jdf:locale-code>
    <jdf:country-code>xx</jdf:country-code>
</jdf:header>
<app:data xmlns:app="xxxxxx">
    <app:EventOut xmlns:ns2="http://www.w3.org/1999/XSL/Transform">
        <app:eventId>xxx</app:eventId>
        <app:distributorId>xxx</app:distributorId>
        <app:distributionNetworkId>xxx</app:distributionNetworkId>
        <app:typology>xxx</app:typology>
        <app:targets>
            <app:target>
                ......
            </app:target>
            <app:target>
                .....
            </app:target>
        </app:targets>
        <app:object>
            <ns2:internalEventObject>
                <ns2:id>!!!!!!!!</ns2:id>
                <ns2:lang1>xxx</ns2:lang1>
            </ns2:internalEventObject>
        </app:object>
        ...
    </app:EventOut>
</app:data>
</jdf:root>

After your code, just use this to get the value.

var value = anode.InnerText; //!!!!!!!!

Let me know if this works!

Sign up to request clarification or add additional context in comments.

2 Comments

I noticed the missing tag, I just assumed OP didn't copy the entire thing by accident. But hey that might be the whole root of the problem.
Yes, it works! Thanks! I had bad namespace added in c# code. Xml dont have end tag because it is no all of xml code.
1

Write down the entire path for that node.

XmlNode anode = xml.SelectSingleNode("/ns2:internalEventObjects/ns2:id", nsmgr);

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.