2

我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<Horizon-Export>
  <BatchNo.>1</BatchNo.>
  <SpecimenID>CL1</SpecimenID>
  <OperatorName>Anuj</OperatorName>
  <SpecimenAge>1.00</SpecimenAge>
  <Grade>M12</Grade>
  <DateofCasting>01/09/2012</DateofCasting>
  <SpecimenShape>Cube</SpecimenShape>
  <SpecimenSize>150.00</SpecimenSize>
  <Area>22,500</Area>
  <Weight>10.0</Weight>
  <Density>1.00</Density>
  <TestDate>17/09/2012</TestDate>
  <TestTime>9:41:08 AM</TestTime>
  <BatchDate>17/09/2012</BatchDate>
  <UltimateForce>
  </UltimateForce>
  <UltimateStress>
  </UltimateStress>
  <Remarks>Pass</Remarks>
  <BatchNo.>1</BatchNo.>
  <SpecimenID>CL1</SpecimenID>
  <OperatorName>Anuj</OperatorName>
  <SpecimenAge>1.00</SpecimenAge>
  <Grade>M12</Grade>
  <DateofCasting>01/09/2012</DateofCasting>
  <SpecimenShape>Cube</SpecimenShape>
  <SpecimenSize>150.00</SpecimenSize>
  <Area>22,500</Area>
  <Weight>10.0</Weight>
  <Density>1.00</Density>
  <TestDate>17/09/2012</TestDate>
  <TestTime>9:47:10 AM</TestTime>
  <BatchDate>17/09/2012</BatchDate>
  <UltimateForce>25.3</UltimateForce>
  <UltimateStress>1.12</UltimateStress>
  <Remarks>Pass</Remarks>
  <BatchNo.>1</BatchNo.>
  <SpecimenID>CL1</SpecimenID>
  <OperatorName>Anuj</OperatorName>
  <SpecimenAge>1.00</SpecimenAge>
  <Grade>M12</Grade>
  <DateofCasting>01/09/2012</DateofCasting>
  <SpecimenShape>Cube</SpecimenShape>
  <SpecimenSize>150.00</SpecimenSize>
  <Area>22,500</Area>
  <Weight>10.0</Weight>
  <Density>1.00</Density>
  <TestDate>17/09/2012</TestDate>
  <TestTime>9:48:57 AM</TestTime>
  <BatchDate>17/09/2012</BatchDate>
  <UltimateForce>8.3</UltimateForce>
  <UltimateStress>0.37</UltimateStress>
  <Remarks>Pass</Remarks>
  <BatchNo.>1</BatchNo.>
  <SpecimenID>CL1</SpecimenID>
  <OperatorName>Anuj</OperatorName>
  <SpecimenAge>1.00</SpecimenAge>
  <Grade>M12</Grade>
  <DateofCasting>01/09/2012</DateofCasting>
  <SpecimenShape>Cube</SpecimenShape>
  <SpecimenSize>150.00</SpecimenSize>
  <Area>22,500</Area>
  <Weight>10.0</Weight>
  <Density>1.00</Density>
  <TestDate>17/09/2012</TestDate>
  <TestTime>9:49:20 AM</TestTime>
  <BatchDate>17/09/2012</BatchDate>
  <UltimateForce>10.9</UltimateForce>
  <UltimateStress>0.49</UltimateStress>
  <Remarks>Pass</Remarks>
  <BatchNo.>1</BatchNo.>
  <SpecimenID>CL1</SpecimenID>
  <OperatorName>Anuj</OperatorName>
  <SpecimenAge>1.00</SpecimenAge>
  <Grade>M12</Grade>
  <DateofCasting>01/09/2012</DateofCasting>
  <SpecimenShape>Cube</SpecimenShape>
  <SpecimenSize>150.00</SpecimenSize>
  <Area>22,500</Area>
  <Weight>10.0</Weight>
  <Density>1.00</Density>
  <TestDate>17/09/2012</TestDate>
  <TestTime>9:49:42 AM</TestTime>
  <BatchDate>17/09/2012</BatchDate>
  <UltimateForce>2.6</UltimateForce>
  <UltimateStress>0.12</UltimateStress>
  <Remarks>Pass</Remarks>
</Horizon-Export>

我的客服:

private List<CubeTestDTL> ParseXMLToList()
    {
        List<CubeTestDTL> cubeTestDetailList = new List<CubeTestDTL>();
        if (fuImport.HasFile)
        {
            if (fuImport.PostedFile.ContentLength > 0)
            {
                var name = string.Format("{0}.xml", Guid.NewGuid().ToString().Replace("-", string.Empty));
                var filePath  = string.Format("~/Temp/{0}",name);
                fuImport.SaveAs(Server.MapPath(filePath));
                cubeTestDetailList =
                (
                    from e in XDocument.Load(Server.MapPath(string.Format("~/Temp/{0}",name))).Root.Elements("Horizon-Export")
                    select new CubeTestDTL
                    {
                        Srlno = (int)e.Element("BatchNo."),
                        Accd = Session["ACCD"].ToString(),
                        AvgCompStrength = 0,
                        BreakingLoad = (double)e.Element("UltimateForce"),
                        CompStrength = (double)e.Element("UltimateStress"),
                        CubeArea = (double)e.Element("Area"),
                        CubeDensity = (double)e.Element("Density"),
                        CubeNo = (string)e.Element("SpecimenID"),
                        CubeWeight = (double)e.Element("Weight"),
                        CustDate = (DateTime)e.Element("DateofCasting"),
                        Grade = (string)e.Element("Grade"),
                        Location = (string)e.Element("OperatorName"),
                        MainAccd =Session["MAINACCD"].ToString(),
                        TargetStrength = 0,
                        TestOn = (double)e.Element("TestDate"),

                    }).ToList<CubeTestDTL>();


            }
        }
        return cubeTestDetailList;

    }

问题:

每次解析返回我 0 个项目。我无法调试,因为整个 linq 块一次通过。列表计数始终显示 =0。

4

2 回答 2

0

从表达式中得不到任何结果是正常的:

from e in (LoadXmlStuff()).Root.Elements("Horizon-Export")

因为在您的 XML<Horizon-Export> 根的情况下,而不是根的后代。

这也意味着 XML 中不能有多个<Horizon-Export>标签,因为 XML 只能有一个根。拥有更多标签的唯一可能性是将它们包装在另一个标签中,然后该标签将成为根。

于 2013-09-20T11:16:11.573 回答
0

你试过换行吗

.Root.Elements("Horizon-Export")

为了

.Descendants("Horizon-Export")

? 不过,我不使用 linq 来查询 XML,我更喜欢使用 foreach 方式,所以这是我的第一个猜测。

于 2013-09-20T09:52:18.383 回答