Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get values from XML with xpath when having Namespace

I am new to xpath matching. Here I have a method which pass XML contain as string. I convert it into XmlDocument.

public static void getProjectDataInfo(string content) {

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(content);
}

Here is my XML. It has xmlns:my

<?xml version="1.0" encoding="UTF-8"?>
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-03T16:54:46" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-us">
  <my:Financial>
      <my:Quote>
     <my:CHARGE_TYPE>MRC</my:CHARGE_TYPE>
     <my:Price>463.92</my:Price>
      </my:Quote>
  </my:Financial>
</my:myField>

I just want to get values of

/my:myFields/my:Financial/my:Quote/my:Price

However I unable to get values hence this XML has xmlns.

Please help me.

like image 465
devan Avatar asked Mar 11 '26 08:03

devan


1 Answers

Use XmlNamespaceManager

XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-03T16:54:46");

var str = doc.XPathSelectElement("/root/ns:myFields/ns:Financial/ns:Quote/ns:Price", nsmgr)
            .ToString(SaveOptions.DisableFormatting);
Console.WriteLine(str);
like image 194
Typist Avatar answered Mar 12 '26 21:03

Typist