[英]Unable to Parse XML using LINQ in ASP.Net & C#
My XML: 我的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>
My CS: 我的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;
}
Problem: 问题:
Every time the parsing returns me 0 items. 每次解析返回0个项目给我。 I am unable to debug as the whole linq block passes at once. 我无法调试整个linq块一次。 List Count is always showing =0. 列表计数始终显示为= 0。
Have you tried switching 您是否尝试过切换
.Root.Elements("Horizon-Export")
for 对于
.Descendants("Horizon-Export")
? ? I do not use linq to query XML though, I prefer going the foreach way, so this would be my first guess. 虽然我不使用linq查询XML,但我更喜欢使用foreach方式,因此这是我的第一个猜测。
It's normal to not get any results from the expression: 不能从表达式中得到任何结果是正常的:
from e in (LoadXmlStuff()).Root.Elements("Horizon-Export")
because in the case of your XML <Horizon-Export>
is the root, not a descendant of the root. 因为对于您的XML, <Horizon-Export>
是根,而不是根的后代。
This also means that there cannot be multiple <Horizon-Export>
tags in the XML, since an XML can only have one root. 这也意味着XML中不能有多个<Horizon-Export>
标记,因为XML只能有一个根。 The only possibility to have more tags would be to wrap them in another tag, which would then become the root. 拥有更多标签的唯一可能性是将它们包装在另一个标签中,然后成为根。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.