Click here to Skip to main content
15,914,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<?xml version="1.0" encoding="UTF-8"?>
<ns2:PkgSt xmlns:ns2="dfyth">
<ns2:test>
<dest>
<ns2:Faclocnr>2782</ns2:Faclocnr>
</dest>
</ns2:test>
</ns2:pkgst>

I have xml data, I will get this data from a string.

I want to extract data of <ns2:faclocnr> , I may get different names spaces from string xml data either ns1 or ns2.

Also, this xml data string may contain this field or may not, for that I have written validation as well.

What I have tried:

C#
protected internal static string XmlLoc(string Datamsg)
        {
            XmlDocument xmlDoc = new XmlDocument();
            var xmlmsg = Datamsg;
            var xmlstring = "[\x00-\x08\x0B\x0C\x0E-\x1F]";
            xmlmsg = Regex.Replace(Datamsg, xmlstring, String.Empty, RegexOptions.Compiled);
            xmlDoc.LoadXml(xmlmsg);
            XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
            manager.AddNamespace("ns2", "dfyth");
                 
            string FacLocNr = DBNull.Value.ToString();
            if (xmlDoc.SelectSingleNode("//dest/FacLocNr", manager) != null)
            {
                FacLocNr = xmlDoc.SelectSingleNode("//dest/FacLocNr", manager).InnerText;
            }

            return FacLocNr;
}
Posted
Updated 15-May-18 8:29am
v5

Quote:
<ns2:faclocnr>
...
"//dest/FacLocNr"

The node is called faclocnr, and is in the dfyth namespace.

It will therefore not match a query for FacLocNr in the default namespace.

You need to include the namespace in your query, and the element name needs to match precisely - XML is case-sensitive.
C#
if (xmlDoc.SelectSingleNode("//dest/ns2:faclocnr", manager) != null)
{
    FacLocNr = xmlDoc.SelectSingleNode("//dest/ns2:faclocnr", manager).InnerText;
}
 
Share this answer
 
Comments
Member 13801408 15-May-18 14:34pm    
Thank you for your response.

I tried this method.(it will work only when Namespace is ns2) My question is what if the namespace is ns1?

The data in the xml string contain different namespaces for different messages
But we don't know what will the be namespace in that string, I may get either
<ns2:faclocnr>2782
or
<ns1:faclocnr>2782
Richard Deeming 15-May-18 15:00pm    
Read the default namespace from the document element:
XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
manager.AddNamespace("a", xmlDoc.DocumentElement.NamespaceURI);

XmlNode node = xmlDoc.SelectSingleNode("//dest/a:faclocnr", manager);
if (node != null)
{
    FacLocNr = node.InnerText;
}
Richard Deeming 15-May-18 15:01pm    
NB: The prefix is irrelevant; it's only the namespace URI that matters.

So long as the prefix in your query maps to the same URI as the namespace declared in the document, it will work.
To add to Rischard's solution:
XML IS case sensitive!!!
So faclocnr not equal to FacLocNr even in the same namespace...
 
Share this answer
 
Comments
Richard Deeming 15-May-18 14:53pm    
I think I already said that, but it's worth repeating. :)
Kornfeld Eliyahu Peter 15-May-18 15:02pm    
I must have blinking... Just missed... You have to write with larger font!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900