3

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.

0

4 Answers 4

5

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);
Sign up to request clarification or add additional context in comments.

2 Comments

I think we can not get '/root/ns:myFields/ns:Financial/ns:Quote/ns:Price' at one time. Insterd of it I get part by part.
Yes. We need to get it element by element. I did not dare to execute it. It was a logical error. Thanks.
0

You need to tell the XmlDocument what my prefix binds to. You use XmlNamespaceManager to do that. Here is an example http://support.microsoft.com/kb/316913. In your case you would do:

xmlns.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-03T16:54:46")

Comments

0

Use LINQ2XML

XElement doc=XElement.Parse(content);
XNamespace my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-03T16:54:46";
var price=doc.Element(my+"myFields").Element(my+"Financial").Element(my+"Quote").Element(my+"Price").Value;

OR[if you want to stick with that OLD thing]

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.OwnerDocument.NameTable);
nsmgr.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-03T16:54:46");
var priceNode = el.SelectNode(@"/my:myFields/my:Financial/my:Quote/my:Price", nsmgr);

Comments

0

For Infopath form 2013, it could be as following:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2014-11-10T18:44:26");

XmlNode root = doc.DocumentElement;

XmlNode yourIDNode = root.SelectSingleNode("/my:myFields/my:YourID", nsmgr);

The detail "my" namespace definition, you could download from Share Point Document Library where your infopath form submitted xml data staying. Download one of those saved infopath form xml data files, and open it with notepad, then you wll find it on the top of content.

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.