2012-05-31 32 views
1

請仔細閱讀下面的代碼和建議什麼需要做是爲了獲得在輸出額外的最後一行,如圖預期輸出整蠱的LINQ to XML

class test { 

    static void Main(string[] args) 
    { 

     XDocument doc = XDocument.Load("E:\\BI_analytics\\Data\\so.xml"); 
     var query = from test in doc.Descendants("tester") 
        from testreq in test.Descendants("testRequest") 
        from testresp in test.Descendants("testResponse") 
        let id = testreq.Element("id") == null ? string.Empty : testreq.Element("id").Value 

        // select id; 
        from itm in testresp.Descendants("item") 
        select new 
        { 
         ID = (string)id, 
         Name = (string)itm.Attribute("itemname"), 
         Code = (string)itm.Attribute("itemocde"), 
        }; 


     foreach (var result in query) 
     { 
      Console.WriteLine(result); 
     } 
    } 
} 

電流輸出

 
{ ID = 2, Name = test item1, Code = 111 } 
{ ID = 2, Name = test item2, Code = 222 } 
{ ID = 3, Name = test item3, Code = 333 } 
{ ID = 3, Name = test item4, Code = 444 } 

預期輸出

 
{ ID = 2, Name = test item1, Code = 111 } 
{ ID = 2, Name = test item2, Code = 222 } 
{ ID = 3, Name = test item3, Code = 333 } 
{ ID = 3, Name = test item4, Code = 444 } 
{ ID = 4, Name = , Code = } 
<?xml version="1.0" encoding="utf-8"?> 
<root> 
    <tester> 
    <testRequest> 
     <id>2</id> 
    </testRequest> 
    <testResponse> 
     <items> 
     <item itemname="test item1" itemocde="111"/> 
     <item itemname="test item2" itemocde="222"/> 
     </items> 
    </testResponse> 
    </tester> 
    <tester> 
    <testRequest> 
     <id>3</id> 
    </testRequest> 
    <testResponse> 
     <items> 
     <item itemname="test item3" itemocde="333"/> 
     <item itemname="test item4" itemocde="444"/> 
     </items> 
    </testResponse> 
    </tester> 
    <tester> 
    <testRequest> 
     <id>4</id> 
    </testRequest> 
    <testResponse> 
     <items /> 
    </testResponse> 
    </tester> 
</root> 
+1

如果您想要設置代碼,結果和XML的格式,這將非常有幫助。 –

+0

感謝巴厘島的建議!將嘗試進一步格式化。 – user219628

回答

2

這就是問題所在:

from itm in testresp.Descendants("item") 

你不任何item元素,所以你可能想:

from itm in testresp.Descendants("item").DefaultIfEmpty() 

...而此時你需要:

select new 
{ 
    ID = (string)id, 
    Name = itm == null ? "" : (string)itm.Attribute("itemname"), 
    Code = itm == null ? "" : (string)itm.Attribute("itemocde"), 
}; 
+0

輝煌!非常感謝。 – user219628

3

我使用一個類來幫助我返回值,即使它是空的,看起來像這樣:

public static class LinqToXMLUtility 
{ 
    /// <summary> 
    /// Used to Get check the XElement Value and return 
    /// empty string if it is null (used for optional or missing xml items) 
    /// </summary> 
    /// <param name="pElement"></param> 
    /// <param name="pstrElementName"></param> 
    /// <returns></returns> 
    public static string GetXElementValue(XElement pElement, string pstrElementName) 
    { 
     string strRet = string.Empty; 
     try 
     { 
      XElement lElement = pElement.Element(pstrElementName); 
      if (lElement != null) 
      { 
       strRet = lElement.Value; 
      } 
     } 
     catch { } 
     return strRet; 
    } 
} 

並像這樣使用它。

class test 
{ 
    static void Main(string[] args) 
    { 
     XDocument doc = XDocument.Load("E:\\BI_analytics\\Data\\so.xml"); 
     var query = from test in doc.Descendants("tester") 
        from testreq in test.Descendants("testRequest") 
        from testresp in test.Descendants("testResponse") 
        let id = testreq.Element("id") == null ? string.Empty : 
          testreq.Element("id").Value 

        // select id; 
        from itm in testresp.Descendants("item") 
        select new 
        { 
         ID = LinqToXMLUtility.GetXElementValue(itm, "id"), 
         Name = LinqToXMLUtility.GetXElementValue(itm, "itemname"), 
         Code = LinqToXMLUtility.GetXElementValue(itm, "itemocde"), 
        }; 


     foreach (var result in query) 
     { 
      Console.WriteLine(result); 
     } 
    } 
}