none
How to get value of my XML element using XPATH RRS feed

  • Question

  • I am using XPATH query as  "//d:Name" which returns me entire element. I only need value of that element which is "{B4FC46E7}#Monks". How to get only value using XPATH. Appreciate your reply. Sample XML below.

    <?xml version="1.0" encoding="utf-8"?>
        <entry>
            <content type="application/xml">
                <m:properties>
                    <d:Description>Feature dependency</d:Description>
                    <d:DisplayCategory m:null="true" />
                    <d:Id m:type="Edm.Int32">1</d:Id>
                    <d:ImageUrl m:null="true" />
                    <d:IsHidden m:type="Edm.Boolean">false</d:IsHidden>
                    <d:IsRootWebOnly m:type="Edm.Boolean">false</d:IsRootWebOnly>
                    <d:IsSubWebOnly m:type="Edm.Boolean">false</d:IsSubWebOnly>
                    <d:Lcid m:type="Edm.Int32">1033</d:Lcid>
                    <d:Name>{B4FC46E7}#Monks</d:Name>
                    <d:Title>Monk</d:Title>
                </m:properties>
            </content>
        </entry>

    • Edited by AlexMathew Wednesday, April 25, 2018 8:28 PM
    Wednesday, April 25, 2018 8:27 PM

Answers

All replies

  • Hello AlexMathew,

    When I load above xml file this will throw exception named "m is an undeclared prefix". After adding a namespace the process of reading single node should like below.

    <?xml version="1.0" encoding="utf-8"?>
    <m:properties xmlns:m="http://www.w3.org/TR/html4/" xmlns:d="http://www.w3.org/TR/html4/">
        <entry>
          <content type="application/xml">
            <m:properties>
              <d:Description>Feature dependency</d:Description>
              <d:DisplayCategory m:null="true" />
              <d:Id m:type="Edm.Int32">1</d:Id>
              <d:ImageUrl m:null="true" />
              <d:IsHidden m:type="Edm.Boolean">false</d:IsHidden>
              <d:IsRootWebOnly m:type="Edm.Boolean">false</d:IsRootWebOnly>
              <d:IsSubWebOnly m:type="Edm.Boolean">false</d:IsSubWebOnly>
              <d:Lcid m:type="Edm.Int32">1033</d:Lcid>
              <d:Name>{B4FC46E7}#Monks</d:Name>
              <d:Title>Monk</d:Title>
            </m:properties>
          </content>
        </entry>
    </m:properties>

    The code

     static void Main(string[] args)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("../../XMLFile1.xml");
                var nsmgr = new XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("m", "http://www.w3.org/TR/html4/");
                nsmgr.AddNamespace("d", "http://www.w3.org/TR/html4/");
    
                var node = doc.SelectSingleNode("//d:Name",nsmgr).InnerText;
            }

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 26, 2018 3:15 AM
    Moderator
  • My bad. I did removed some tags to hide some of my server information. I need only XPATH to get a value as I am using it in Nintex workflow. So, i can not write .NET code to get it. I am using online tool to test my XPATH is 

    https://www.freeformatter.com/xpath-tester.html

    I need the innertext as output. Appreciate your help. Thanks!

    Thursday, April 26, 2018 1:48 PM
  • Try this expression:

       //d:Name/text()

    • Marked as answer by AlexMathew Friday, May 11, 2018 6:51 PM
    Thursday, April 26, 2018 5:22 PM