我的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>
我的CS:
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。
答案 0 :(得分:0)
您是否尝试过切换
.Root.Elements("Horizon-Export")
的
.Descendants("Horizon-Export")
?我不使用linq查询XML,我更喜欢使用foreach方式,所以这是我的第一个猜测。
答案 1 :(得分:0)
从表达式中得不到任何结果是正常的:
from e in (LoadXmlStuff()).Root.Elements("Horizon-Export")
因为在您的XML <Horizon-Export>
的情况下是根,而不是根的后代。
这也意味着XML中不能有多个<Horizon-Export>
标记,因为XML只能有一个根。拥有更多标签的唯一可能性是将它们包装在另一个标签中,然后该标签将成为根。