在C#中解析XML不分枝 [英] Parsing unbranched XML in C#
问题描述
我在下面的格式获得XML从嵌入式机器:
I am getting XML from a embedded machine in the below format:
<?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>
它实际上是一种在一个单独的XML多个测试结果测试输出。 AFAIK的XML有一些错误的格式,因为所有的测试是在一个单一的水平并且它们不支链的。为了使该XML可读我把结果集之间的线。 BatchNo&GT;&LT; /BatchNo.> ,并在结束&LT;说明&GT;&LT; /备注&GT; $ A测试结果从
&LT启动C $ C>。我有同样的一类。对于单个结果集或支链的结果,我可以分析,但在这种情况下,我的code解析只有一次。我需要创建类相同的列表。
It is actually test output having multiple test result in a single xml. AFAIK the xml has some wrong format as all the test are in a single level and they are not branched. For making the XML readable i put line between the result sets. A test result starts from <BatchNo></BatchNo.>
and ends at <Remarks></Remarks>
. I have a class for the same. For a single result set or branched result I can parse but in this case my code parse only once. I need to create a list of class of the same.
code我使用的:
var root = XDocument.Load(path).Root;
var s = root.Element("BatchNo.").value; // and so on for other nodes.
我有类似的问题贴出来,因为我不知道从客户的实际需求。现在他们说,它不是一个测试其实际结果的多个测试,所以我再次发布问题。请不要票关闭或downvote。
I have similar question posted, as i was unaware of the actual requirement from the client. Now they said its not one test its actually result of multiple tests, so I am posting question again. Kindly do not vote close or downvote.
<一个href=\"http://stackoverflow.com/questions/18913311/unable-to-parse-xml-using-linq-in-asp-net-c-sharp\">Unable在ASP.Net和功放使用LINQ to XML解析; C#
推荐答案
试试这个
class MX
{
public string BatchNo { get; set; }
public string SpecimenID { get; set; }
//and so on
static public List<MX> Arr = new List<MX>();
}
protected void Page_Load(object sender, EventArgs e)
{
XDocument doc = XDocument.Load(Server.MapPath("~/xml/a.xml"));
List<string> ListBatchNo = new List<string>();
foreach (var node in doc.Descendants("BatchNo."))
{
ListBatchNo.Add(node.Value);
}
List<string> ListSpecimenID = new List<string>();
foreach (var node in doc.Descendants("SpecimenID"))
{
ListSpecimenID.Add(node.Value);
}
MX.Arr.Clear();
for (int i = 0; i < ListBatchNo.Count; i++)
{
MX obj = new MX();
obj.BatchNo = ListBatchNo[i];
obj.SpecimenID = ListSpecimenID[i];
MX.Arr.Add(obj);
}
GridView2.DataSource = MX.Arr;
GridView2.DataBind();
}
这篇关于在C#中解析XML不分枝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!