Question

I have never used XML much and need some help reading a complex file. enter image description here

I am trying to get to the number that is nested in: outputTree\command\heading\pivotTable\dimension\category\dimension\category\dimension\group\category\call:Text(number)

I just have no idea how to do it. if anyone could show me some basics of this it would be helpful all the examples I see are very basic and don't have this many levels so I am having issues understanding how to get to this level.

thank you.

Was it helpful?

Solution 2

You can use an XPath query. Based on the structure of your document, this should snag you the precise value you are looking for:

var xml = XDocument.Load(@"c:\path\to\file.xml");
var nsManager = new XmlNamespaceManager(new NameTable());

nsManager.AddNamespace(
    "oms",
    "http://www.ibm.com/software/analytics/spss/xml/oms");

var cell = xml.XPathSelectElement(
    "oms:outputTree/oms:command[@text='Regression']/" +
    "oms:heading[@text='uid = 1015984.00']/" +
    "oms:pivotTable[@text='Coefficients']/" +
    "oms:dimension/oms:category/oms:dimension/oms:category/" +
    "oms:dimension/oms:group/oms:category/oms:cell",
    nsManager);

var number = (decimal)cell.Attribute("number");

Console.WriteLine(number);

Tweak as necessary. You will need these imports:

using System.Xml.Linq;
using System.Xml.XPath;

OTHER TIPS

As in this answer, you can select the XML Node directly using an advanced XPath filter

such as:

var document = new XmlDocument();
document.Load("<fileName>");
XmlNode node = document.SelectSingleNode("outputTree/command/heading[@text='uid = 1015984.00']/pivotTable[@text='Coefficients']/dimension[@text='Model']");

etc.

see this for a list of examples: http://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx

I think I am going to end up doing this and add some if statements in to catch the data I am looking for:

XmlTextReader reader = new XmlTextReader("c:/temp/descriptives_table.xml");
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element: // The node is an element.
                        Console.Write("<" + reader.Name);

                        while (reader.MoveToNextAttribute()) // Read the attributes.
                            Console.Write(" " + reader.Name + "='" + reader.Value + "'");
                        Console.WriteLine(">");
                        break;
                    case XmlNodeType.Text: //Display the text in each element.
                        Console.WriteLine(reader.Value);
                        break;
                    case XmlNodeType.EndElement: //Display the end of the element.
                        Console.Write("</" + reader.Name);
                        Console.WriteLine(">");
                        break;
                }
            }

Thank you all for your help! and leading me to this path!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top