如何让4个孩子成为变量

时间:2014-01-21 14:39:54

标签: c# xml xpath

我试图使用XPath从XML文档中获取一些变量问题是它们每次都是不同的(变量)但是总有4个我不知道如何从XML中获取数据文献。 enter image description here

我对XML或XPath不是很好,但这是我到目前为止所提出的问题,因为我不知道如何循环使用单元格来获取数据。

    for (int i = 0; i < 3; i++)
                {
                    string sElementInfo = string.Empty;
                    var cell = xml.XPathSelectElement(
                    "oms:outputTree/oms:command[@text='Discriminant']/" +
                    "oms:heading[@text='Analysis 1']/" +
                    "oms:heading[@text='Classification Statistics']/" +
                    "oms:pivotTable[@text='Classification Function Coefficients']/" +
                    "oms:dimension[@text='(Variables)']",
                    nsManager);

                    if (cell != null)
                    {
                        sStatement1 = (string)cell.Attribute("varName");
                    }

                }

e4,e17和e22每次都会有所不同,所以我找不到那个名字,盒装的“数字”也会每次都不同,所以我也不知道怎么做。 我虽然如果我循环整个文件,但后来我无法弄清楚如何到达下一个单元格。任何帮助都会很棒! 谢谢。

如果这有助于这是我通常用xpath做的事情,问题是我不知道varName会是什么,所以我可以挑选出其余的数据,这是来自不同的XML文档,但类似:

    var xml = XDocument.Load(@"c:/temp/descriptives_table.xml");
            var nsManager = new XmlNamespaceManager(new NameTable());

            nsManager.AddNamespace(
                "oms",
                "http://www.ibm.com/software/analytics/spss/xml/oms");
            try
            {
                SqlDataReader sqlDR = sqlCom.ExecuteReader();
                while (sqlDR.Read())
                {
                    string sElementInfo = string.Empty;
                    var cell = xml.XPathSelectElement(
                    "oms:outputTree/oms:command[@text='Regression']/" +
                    "oms:heading[@text='uid = " + sqlDR.GetDouble(0) + ".00']/" +
                    "oms:pivotTable[@text='Coefficients']/" +
                    "oms:dimension/oms:category/oms:dimension/oms:category[@text='(Constant)']/" +
                    "oms:dimension/oms:group/oms:category/oms:cell",
                    nsManager);
                    if (cell != null)
                    {
                        var number = (string)cell.Attribute("number");
                        sElementInfo = sqlDR.GetDouble(0) + ", " + number + ", ";
                        //Console.WriteLine(sqlDR.GetDouble(0).ToString() + " (Constant) " + number);
                        for (int i = 1; i < 37; i++)
                        {

                            var xElement = xml.XPathSelectElement(
                            "oms:outputTree/oms:command[@text='Regression']/" +
                            "oms:heading[@text='uid = " + sqlDR.GetDouble(0) + ".00']/" +
                            "oms:pivotTable[@text='Coefficients']/" +
                            "oms:dimension/oms:category/oms:dimension/oms:category[@text='e" + i +"']/" +
                            "oms:dimension/oms:group/oms:category/oms:cell",
                            nsManager);
                            if (xElement != null)
                            {
                                var nElement = (string)xElement.Attribute("text");
                                sElementInfo = sElementInfo + nElement + ", ";
                                //Console.WriteLine(sqlDR.GetDouble(0).ToString() + " e" + i + " " + nElement);
                                //Console.WriteLine(" ");
                            }
                        }
                        //Console.WriteLine(sElementInfo.Remove(sElementInfo.Length-2));

1 个答案:

答案 0 :(得分:2)

您可以尝试使用System.Xml.Linq

        var doc = XDocument.Load("XMLFile1.xml");

        XNamespace ns = @"http://www.ibm.com/software/analytics/spss/xml/oms";

        var pivotTables = doc.Descendants(ns + "pivotTable");

        // identify the pivottable by the text "Classified Function Coefficients"
        var theTable = pivotTables.Where(table => table.Attribute( "text").Value == "Classification Function Coefficients");

        // first dimension
        var dimension = theTable.Elements(ns + "dimension").First();

        // first dimensions categories
        var categories = dimension.Elements(ns + "category");

        foreach (var category in categories)
        {
            // at index 5 there exists a category without varname and text="(Constant)"
            // skip it
            if(!category.Attributes().Any(attr => attr.Name == "varName"))
                continue;

            // here's the varname                
            var varName = category.Attribute( "varName").Value;

            if (varName.StartsWith("("))
                continue;

            System.Diagnostics.Debug.WriteLine(varName);
            var cells = category.Descendants(ns + "cell");

            foreach (var cell in cells)
            {
                // here's the number of the cell
                var number = cell.Attribute("number").Value;
                System.Diagnostics.Debug.WriteLine(number);
            }
        }

输出:

  • E9
  • 0.16392050172531
  • 0.13063860362111
  • 0.28542086095635

  • E18

  • 0.32758593355552
  • 0.071751070799144
  • 0.4532773907448

  • E33

  • 0.16067704682981
  • 0.254227301797
  • 0.01461475905339

  • E34

  • 0.3033365693718
  • 0.12043800410649
  • 0.58905116575338