在 C# 中解析无分支的 XML

本文关键字:分支 XML | 更新日期: 2023-09-27 18:32:18

我从嵌入式机器中获取以下格式的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>

它实际上是在单个 xml 中具有多个测试结果的测试输出。AFAIK XML 有一些错误的格式,因为所有测试都在一个级别中,并且它们没有分支。为了使 XML 可读,我在结果集之间放了线。测试结果从 <BatchNo></BatchNo.> 开始,到 <Remarks></Remarks> 结束。我有一个相同的课程。对于单个结果集或分支结果,我可以解析,但在这种情况下,我的代码只解析一次。我需要创建一个相同的类列表。

我正在使用的代码:

var root = XDocument.Load(path).Root;
var s = root.Element("BatchNo.").value; // and so on for other nodes.

我发布了类似的问题,因为我不知道客户的实际要求。现在他们说这不是一个测试,而是多个测试的实际结果,所以我再次发布问题。请不要投反对票或反对票。

无法在 ASP.Net 和 C# 中使用 LINQ 解析 XML

在 C# 中解析无分支的 XML

试试这个

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();
}

应该很容易转换为更易于使用的 xml。 像这样的东西,它使xml中的每个数据项都成为<item>,因此解析的XML成为列表:

string startStr = "..."; // as above read it or whatever.
string validXML = startStr
   .Replace("<Horizon-Export><BatchNo.>","<Horizon-Export><item><BatchNo.>")
   .Replace(@"</Remarks><BatchNo.>",@"</Remarks></item><item><BatchNo.>")
   .Replace(@"</Remarks></Horizon-Export>",@"</Remarks></item></Horizon-Export>");

您可能需要根据源字符串的确切格式进行调整,但这是一种简单的算法;对于列表中的第一项,您需要以 <item> 作为前缀。 在每个项目之间添加</item><item>,然后将</item>放在列表的末尾。

我知道你与此无关,但我必须指出,在XML标签和属性名称中混合大写和小写被认为是非常糟糕的形式。 所有 xml 标记都应为小写。