2013-05-31 117 views
2

我有XML設置這樣的LINQ to XML:查詢。如果塊C#

<customers> 
    <customer id="1"> 
    <name title="Mr" first="John" last="Smith" /> 
    <contact number="07123123123" email="[email protected]" /> 
    <address postcode="E1 1EW">1 Paper Street, London, England, GB</address> 
    </customer> 
    (...) 
</customers> 

我試圖使用LINQ查詢它的XML學習之用。到目前爲止,我可以XDocument.Load文件罰款,並添加/刪除等。但我似乎無法找出一種方法來查詢我的XML文檔在if塊中使用。例如像(僞代碼):

XDocument document = XDocument.Load("People.xml"); 

if(
    exists(
    document.customers.customer.name.first.value("john") 
    && document.customers.customer.name.last.value("smith") 
) 
) 
{ 
    bool exists = true; 
} 

無論我嘗試一些編譯器會無論是在笑我或吐出了一些關於如何不能隱含一個IEnumerable布爾轉換爲布爾。

我一直在嘗試從許多不同的谷歌搜索一段時間的事情的許多組合,我認爲猜測開始做更多的傷害比好,任何人都可以提供一個C#代碼段,將工作在一個if塊爲我xml安裝程序?只是爲了查看在同一節點中是否存在姓和名。通常一旦我看到真正有用的東西,我可以從那裏拿走它。我在網上發現的大多數問題都只是搜索整個節點是否存在,或者只搜索一個屬性,而我似乎無法調整它。

(之前有人闖進了我的XML結構的火焰,這是不是爲生產目的,我只是想獲得的情況下,我需要在即將到來的項目中使用這個的把握。)

獎金愛任何人都可以鏈接任何不是M​​SDN的文檔(我已經打開了大約10個標籤頁),並且涉及到一些關於Linq to XML的低級/初級教程。

+0

你嘗試過什麼?這樣我們不僅可以發佈正確的答案,還可以告訴你錯在哪裏。 –

+0

我完全理解這對你們有幫助,但是我現在已經陪陪陪審團幾個小時,然後從挫敗中刪除它來嘗試下一件事,但我現在無法詳細瞭解我已經完成了,因爲我沒有明確地跟蹤它。無論如何,它可能會是可怕的惡意代碼。 – Dicckk

+0

您需要先查詢'Document.Root' – IAbstract

回答

3

要檢查約翰·史密斯存在在你的XML客戶,你會使用

XDocument doc = XDocument.Load(Path); 
var customers = doc.Descendants("customer");  
//To Check for John Smith  
if (customers.Elements("name") 
      .Any(E => E.Attribute("first").Value == "John" 
        && E.Attribute("last").Value == "Smith")) 
{ 
    //Do your thing 
} 
+0

工作得非常好。非常感謝! – Dicckk

+0

不客氣。 – arunlalam

+0

你有什麼想法如何將郵政編碼包括在內? (E => E.Element(「name」)。Attribute(「first」)。Value ==「John」&& E.Element(「address」)這是從first/last屬性開始的級別。 .Attribute(「postcode」)。Value ==「E1 1EW」)似乎不起作用。 – Dicckk

0

XPath是你的朋友

using System.Xml.XPath; 
... 

void foo(){ 
    XDocument document = XDocument.Load("People.xml"); 

    var firstCustomerNode = document.XPathSelectElement(
     "/customers/customer[0]/name" 
     ); 
    var hasfirstNameAndLastName = firstCustomerNode.Attribute("firstname") != null && firstCustomerNode.Attribute("lastname") != null; 

    if(hasfirstNameAndLastName) 
    { 

    } 
} 

請注意,你也可以使用一個模式來驗證XML,但它是更爲複雜的編寫。

如果你不想使用XPath,您也可以寫:

var firstCustomerNode = document.Root.Element("Customers").Elements("customer").First().Element("name"); 

但說實話,XPath查詢更具可讀性,並簡化錯誤檢查。該代碼假設所有到目標節點的元素都存在。如果沒有,您的代碼將失敗,並顯示差NullReferenceException

+0

這不是LINQ-2-Xml – IAbstract

+0

XPath?真的嗎?不是LINQ to XML ... –

+0

此方法在'System.Xml.Linq'程序集中定義。所以它是鏈接到XML ...但它有關係嗎?你會用螺絲刀畫嗎? –

0

創建一個布爾稱爲校驗容納結果。 做多少個元素叫名字有屬性首先=約翰和最後=史密斯 然後把結果以檢查變量計數。

BTW你的XML是缺少「在約翰面前。這將導致你的問題:)

bool check; 
var res = XDocument.Load(@"c:\temp\test.xml"); 
var results = res.Descendants("name") 
       .Where(x => x.Attribute("first").Value == "john" && x.Attribute("last").Value == "smith") 
       .Select(x => x.Elements()).Count(); 

check = results != 0; 
0

您需要通過查詢Document.Root.Descendants啓動。然後,方法如IfExists

var nodes = document.Root.Descendants("customer"); 

bool IfExists(string FirstName, string LastName) { 
    return nodes.Elements("name").Any(node => 
     node.Attribute("first").Value.Equals(FirstName) && 
     node.Attribute("last").Value.Equals(LastName)); 
} 

您可能需要添加異常處理的情況下,一個屬性丟失或包含空值。