2013-09-21 27 views
1

我在下面的格式獲取XML從嵌入式機器:解析XML無支鏈的C#

<?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. 

我有類似的問題貼出來,因爲我不知道的從客戶的實際需求。現在他們說它不是一次測試它實際上多次測試的結果,所以我再次發佈問題。請不要投票結束或倒退。

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

+0

你能解釋更多請問你的意思是「對於單個結果集或分支結果我可以解析,但在這種情況下,我的代碼只解析一次,我需要創建一個相同類的列表 –

回答

1

試試這個

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

@Amit Ranjan - 我的回答編輯 –

+0

這是我正在尋找。像魅力一樣工作.. –

0

應該很容易轉換爲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標籤都應該是小寫的。