Parsing unbranched XML in C#

妖精的绣舞 提交于 2019-12-11 11:24:20

问题


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>

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 I am using:

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

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.

Unable to Parse XML using LINQ in ASP.Net & C#


回答1:


Try this

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



回答2:


Should be easy to transform into xml that is easier to work with. Something like this which makes each of the data items in the xml an <item> and thus the parsed XML a list:

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>");

You may need to tweak this depending on the exact format of the source string, but it is a simple algorithm; for the first item in the list you need to prefix with <item>. Between each item add </item><item> and then put </item> at the end of the list.

I know you have nothing to do with this but I have to point out that mixing upper and lower case in XML tags and attribute names is considered really bad form. All xml tags should be lower case.



来源:https://stackoverflow.com/questions/18931480/parsing-unbranched-xml-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!