-1

I have XML file which i want to read through xpath in C#

My XML will look like:

<?xml version="1.0"?>
<doc>
     <members>
        <member test="testing" name="T:QuexstBase.Tools.RegistryHelper.RegistryHelper">
            <summary>
            RegistryHelper class to use registry operations.
            </summary>
        </member>
        <member test="testing" name="F:QuexstBase.Tools.RegistryHelper.RegistryHelper.baseKey">
            <summary>
            private member base key
            </summary>
        </member>
        <member  test="tester"  name="F:QuexstBase.Tools.RegistryHelper.RegistryHelper.subKey">
            <summary>
            default sub key
            </summary>
        </member>
    </members>
</doc>

I want to read /members/member/@test whose value is testing. I don't want to use foreach or for loop for these.

Mario S
  • 11,715
  • 24
  • 39
  • 47
Rahul Rajput
  • 1,427
  • 3
  • 17
  • 38
  • 1
    And why do you particularly want to use XPath? Are you getting the XPath itself from somewhere else? (I only ask as I find writing queries in LINQ to XML significantly simpler than XPath...) – Jon Skeet Oct 30 '12 at 07:18
  • The XPathDocument class provides a fast, read-only, in-memory representation of an XML document using the XPath data model that's why i want to use xpath – Rahul Rajput Oct 30 '12 at 07:23
  • do you want to read names only? what to you mean by "I don't want to use foreach loop"? what output do you want to get at the end? – Roman Pekar Oct 30 '12 at 07:53

3 Answers3

3
var xDoc = XDocument.Parse(xml);//or XDocument.Load(fileName)
var members = xDoc.XPathSelectElements("//member[@test='testing']")
                  .ToList();
L.B
  • 114,136
  • 19
  • 178
  • 224
1
var xml = @"<doc>
        <members>
        <member test=""testing"" name=""T:QuexstBase.Tools.RegistryHelper.RegistryHelper"">
            <summary>
            RegistryHelper class to use registry operations.
            </summary>
        </member>
        <member test=""testing"" name=""F:QuexstBase.Tools.RegistryHelper.RegistryHelper.baseKey"">
            <summary>
            private member base key
            </summary>
        </member>
        <member  test=""tester""  name=""F:QuexstBase.Tools.RegistryHelper.RegistryHelper.subKey"">
            <summary>
            default sub key
            </summary>
        </member>
    </members>
</doc>";

var ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml));
var doc = new XPathDocument(ms);
var nav = doc.CreateNavigator();
var nodes = nav.Select("//member[@test='testing']");

update: take a look at this post XDocument or XmlDocument

Community
  • 1
  • 1
Roman Pekar
  • 107,110
  • 28
  • 195
  • 197
1

Try to use XmlDocument:

XmlDocument document = new XmlDocument();
document.LoadXml("your xml string");
var nodes = document.SelectNodes("/members/member/@test");
//or for single node  
var node = document.SelectSingleNode("/members/member/@test");
Ria
  • 10,237
  • 3
  • 33
  • 60
  • ya but selectNodes will give me list of nodes and i already mention i don't want to use foreach loop – Rahul Rajput Oct 30 '12 at 07:42
  • Ria it will give me only 1st node value if you look at xml /members/member/@test there are 3 values of test and i want the list of values of 'testing' – Rahul Rajput Oct 30 '12 at 08:27