2017-06-05 18 views
0

私はXML文字列を受け取り、internalName文字列と比較するWebサービスを開発しています。私はXMLを解析するためにLINQを使用していますが(正しくはそうしていると思いますが)例の中の "value" internalNameを比較する方法がわかりません。解析されたXMLと文字列の比較

[WebMethod] 
    public string WMCompare (string xml, string internalName) 
    { 
     XDocument xmlDoc = XDocument.Load(xml); 

     var result = from ele in xmlDoc.Descendants("property") 
        select new 
        { 
         key = (string)ele.Element("key"), 
         value = (string)ele.Element("value") 
        }; 
     foreach (var i in result) 
     { 
     } 
    } 
} 

初心者のご質問ありがとうございました。 XMLを使ったのは初めてのことです。

+0

あなたが「internalName」と比較したいどのノード/属性値?それは「鍵」か「価値」か? – saurabh

+0

xmlをバインドするモデルを作成するのは難しいですか?次に、XMLをオブジェクトに逆シリアル化して、すべてのプロパティに簡単にアクセスできます。 –

答えて

0

あなたが値を持つ文字列比較していることを考えると:また

var newResult = result.Where(r => r.value.Equals(internalName)) 

をあなたのXMLを解析中に、あなたはまた、比較することができる:

var result1 = from ele in doc.Descendants("property") 
       where ele.HasElements && ele.Element("value") != null && ele.Element("value").Equals(internalName) 
       select new 
       { 
        key = (string)ele.Element("key"), 
        value = (string)ele.Element("value") 
       }; 
関連する問題