Question

I'm trying to select nodes using xpath in c#

This is my XML file

<?xml version="1.0" encoding="utf-8"?>
<xObject version="3.0.2002.0" xmlns="http://schemas.microsoft.com/wix/2006/objects">
    <section id="*" type="product">
        <table name="NotThis">
                <row sourceLineNumber="D:\bla\bla\">
                    <field>Borderish.fo</field>
                    <field>Documents</field>
                    <field>1</field>
                    <field>No, not this line here 1</field>
                </row>
                <row sourceLineNumber="D:\blah\blah\">
                    <field>Charterish</field>
                    <field>Documents</field>
                    <field>1</field>
                    <field>No not, this line here 2</field>
                </row>
            </table>
        <table name="XFile">
            <row sourceLineNumber="D:\bla\bla\">
                <field>Borderish.fo</field>
                <field>Documents</field>
                <field>1</field>
                <field>This line here 1</field>
            </row>
            <row sourceLineNumber="D:\blah\blah\">
                <field>Charterish</field>
                <field>Documents</field>
                <field>1</field>
                <field>This line here 2</field>
            </row>
        </table>
    </section>
</xObject>

This is my C# code which seems to not work

XmlDocument doc = new XmlDocument();
        doc.Load("Testing.xml");
        XmlNode root = doc.DocumentElement;

        XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
        nsmgr.AddNamespace("ns", "xmlns=&quot;http://schemas.microsoft.com/wix/2006/objects&quot;");

        XmlNodeList nodeList = root.SelectNodes("ns:table[@type='XFile']/ns:row", nsmgr);

        foreach (XmlNode xn in nodeList)
        {
            string fieldLine = xn["Field"].InnerText;
            Console.WriteLine("Field: {4}", fieldLine);
        }

What I want to output is every 4th field table name="xfile", like this:

This line here 1

This line here 2

Please let me know if you know a solution or a better way.

Was it helpful?

Solution

First - you should provide just uri for namespace:

nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/wix/2006/objects");

Second - you should use namespace when providing node name. And table has attribute name instead of type:

XmlNodeList nodeList = root.SelectNodes("//ns:table[@name='XFile']/ns:row", nsmgr);

And last - after selecting row nodes, you should select fourth field node (which has full name ns:field):

foreach (XmlNode row in nodeList)
{
    XmlNode field = row.SelectSingleNode("(ns:field)[4]", nsmgr);
    Console.WriteLine("Field: {0}", field.InnerText);
}

Output:

Field: This line here 1
Field: This line here 2

NOTE: You can get fields directly, without looping on rows:

XmlNodeList fields =
    root.SelectNodes("//ns:table[@name='XFile']/ns:row/ns:field[4]", nsmgr);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top