eMRe
eMRe

Reputation: 3247

Load xml file and get the required data with c#

It is the first time i am working with an xml file and i dont know what i am doing.

this is the xml file:

<?xml version="1.0" encoding="UTF-8"?>
<outputTree xmlns="http://www.ibm.com/software/analytics/spss/xml/oms" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ibm.com/software/analytics/spss/xml/oms http://www.ibm.com/software/analytics/spss/xml/oms/spss-output-1.8.xsd">
  <command command="Summarize" displayOutlineValues="label" displayOutlineVariables="label" displayTableValues="label" displayTableVariables="label" lang="en" text="Summarize">
    <pivotTable subType="Report" text="Batch Processing Report">
      <dimension axis="row" text="Cases">
        <group label="Test Site" text="Test Site" varName="PLANT_DESC" variable="true">
          <group hide="true" text="A">
            <group string="UK" text="Scotland" varName="_DESC">
              <group label="Product" text="Product" varName="PROD_DESC" variable="true">
                <group hide="true" text="A">
                  <group string="NameProduct" text="Name" varName="PROD_DESC">
                    <group label="Batch Number" text="Batch Number" varName="BATCH_NO" variable="true">
                      <group hide="true" text="A">
                        <group number="3692649" text="3692649" varName="BATCH_NO">
                          <category number="1" text="1">
                            <dimension axis="column" text="Variables">

                              <Test ID="1"/>
                              <Test ID="2"/>

                              <category label="Batch Run" text="Batch Run" varName="BATCH_RUN_ID" variable="true">
                                <cell number="4242" text="4242" varName="BATCH_RUN_ID"/>
                              </category>
                              <category label="Application" text="Application" varName="APP_ID" variable="true">
                                <cell label="Calibration" number="101" text="Calibration" varName="APP_ID"/>
                              </category>
                              <category label="Date Tested" text="Date Tested" varName="TEST_DATE" variable="true">
                                <cell date="2014-09-24T10:17:04" format="date" text="24-SEP-2014" varName="TEST_DATE"/>
                              </category>
                            </dimension>
                          </category>
                          <category number="2" text="2">
                            <dimension axis="column" text="Variables">
                              <category label="Batch Run" text="Batch Run" varName="BATCH_RUN_ID" variable="true">
                                <cell number="4256" text="4256" varName="BATCH_RUN_ID"/>
                              </category>
                              <category label="Application" text="Application" varName="APP_ID" variable="true">
                                <cell label="Calibration" number="101" text="Calibration" varName="APP_ID"/>
                              </category>
                              <category label="Date Tested" text="Date Tested" varName="TEST_DATE" variable="true">
                                <cell date="2014-09-24T10:53:12" format="date" text="24-SEP-2014" varName="TEST_DATE"/>
                              </category>
                            </dimension>
                          </category>  
                        </group>
                      </group>
                    </group>
                  </group>
                </group>
              </group>
            </group>
          </group>
        </group>
      </dimension>
    </pivotTable>
  </command>
</outputTree>

I have just added the test category <Test ID="1"/> in the xml file to see if i could get any data and it works perfect.

    var doc = XDocument.Load(@"C:\Users\by\Desktop\SITE\s.xml");
    XNamespace rss = "http://www.ibm.com/software/analytics/spss/xml/oms";

    foreach (var item in doc.Descendants(rss + "Test"))
    {
        xmlTitle.Text += "</br>" + item.Attribute("ID").Value + " </br>";
    }

My question is:

How can i get everything under:

<group label="Batch Number" text="Batch Number" varName="BATCH_NO" variable="true">

Upvotes: 0

Views: 152

Answers (3)

eMRe
eMRe

Reputation: 3247

I have managed to solve my problem but took about 1 day. I had to write every element from root to the bottom and include the namespace.

private void GetXMLData()
{
    try
    {
        xmlTitle.Text = "";
        XNamespace ns = "http://www.ibm.com/software/analytics/spss/xml/oms";
        XDocument testXML = XDocument.Load(@"C:\Users\by\Desktop\SITE\ori.xml");

        var cats = from cat in testXML.Element(ns + "outputTree")
                       .Elements(ns + "command")
                       .Elements(ns + "pivotTable")
                       .Elements(ns + "dimension")
                       .Elements(ns + "group")
                       .Elements(ns + "group")
                       .Elements(ns + "group")
                       .Elements(ns + "group")
                       .Elements(ns + "group")
                       .Elements(ns + "group")
                       .Elements(ns + "group")
                       .Elements(ns + "group")
                       .Elements(ns + "group")
                       .Elements(ns + "category")
                       select new
                       {
                           parentC = cat.Parent.Attribute("number").Value,
                           id = cat.Attribute("number").Value,                            
                       };

        foreach (var cat in cats)
        {
            xmlTitle.Text += "</br>Parent: " + cat.parentC + " and ";
            xmlTitle.Text += "Cat: " + cat.id + " </br>";
        }


    }
    catch (Exception err)
    {
        throw (err);
    }
}

Upvotes: 0

xiaoyifang
xiaoyifang

Reputation: 959

use the following code:

        var node = doc.XPathSelectElement("//group[@label='Batch Number']");
        foreach (var item in node.Descendants())
        {
            //sb.Append(item);
        }

Upvotes: 0

decPL
decPL

Reputation: 5402

Assuming you want all elements under a group element with those precise attribute/values, something like this should suffice:

var result = doc.Descendants(rss + "group")
                .Single(e => e.Attribute("label") != null
                          && e.Attribute("label") == "Batch Number"
                          && e.Attribute("text") != null
                          && e.Attribute("text") == "Batch Number"
                          && /* ... */)
                .Descendants();

Note: this assumes exactly one such element is present (Single() throws an exception otherwise).

Upvotes: 1

Related Questions