2

I have a snippet of an xml file that looks like this:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="wa_xml2html.xsl"?>
<computeraudit>
    <title>Computer Audit :: 11/13/2012 10:43:22 AM</title>
    <category title="Loaded Modules">
        <subcategory title="">
            <recordset title="">
                <fieldname>Name</fieldname>
                <fieldname>Version</fieldname>
                <fieldname>Modified</fieldname>
                <fieldname>Manufacturer</fieldname>
                <datarow>
                    <fieldvalue>XmlLite.dll</fieldvalue>
                    <fieldvalue>1.3.1000.0</fieldvalue>
                    <fieldvalue>7/13/2009 8:16:21 PM</fieldvalue>
                    <fieldvalue>Microsoft Corporation</fieldvalue>
                </datarow>              
                <datarow>
                    <fieldvalue>zip.dll</fieldvalue>
                    <fieldvalue>6.0.250.6</fieldvalue>
                    <fieldvalue>5/25/2011 8:30:12 AM</fieldvalue>
                    <fieldvalue>Sun Microsystems, Inc.</fieldvalue>
                </datarow>                      
            </recordset>
        </subcategory>
    </category>         
</computeraudit>    

I'm trying to parse it with C# XPath and get each of the fieldvalue element values.

The c# code looks like this:

        XPathNavigator nav;
        XPathDocument docNav;
        XPathNodeIterator NodeIter;
        String strExpression;
        docNav = new XPathDocument(@"C:\TEMP\WinAudit\test.xml");

        nav = docNav.CreateNavigator();

        strExpression = "/computeraudit/category[@title=\"Loaded Modules\"]/subcategory/recordset/datarow";

        NodeIter = nav.Select(strExpression);

        while (NodeIter.MoveNext())
        {
            Console.WriteLine(NodeIter.Current.Value);
        }
        Console.ReadLine();

It returns one line for each datarow node with all of the fieldvalue element values concatenated together.

How do I return each of the distinct values for each fieldvalue element?

2 Answers 2

1

Change your XPath expression to:

"/computeraudit/category[@title=\"Loaded Modules\"]/subcategory/recordset/datarow/fieldvalue"
Sign up to request clarification or add additional context in comments.

Comments

1

Your selector is only selecting the datarow elements.

Try strExpression = "/computeraudit/category[@title=\"Loaded Modules\"]/subcategory/recordset/datarow/fieldvalue";

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.