2009-04-21 18 views
1

私は、XMLの以下の部分からShippingMethodと属性CodeDestinationを見つける必要がある:私はLINQの持つデータをXMLすることを取得するにはどうすればよいLinqを使用してXMLにデータを取得するにはどうすればよいですか?

<ScoreRule> 
    <ShippingMethod Code="UPS1DA"> 
     <Destination Country="US" Area="IL" Value="0" /> 
    </ShippingMethod> 
</ScoreRule> 

+0

構文を探していますか?実装について具体的な質問がありましたか?実装を見たい言語をお持ちでしたか?どこから始めるべきかわからないのですか? –

答えて

2

ここでは、選択するXMLクエリ式へのリンクです。

最初のデータをどのように読み込んでいるのかわかりませんでしたので、ドキュメントに解析しましたが、データの取得方法に合わせてXDocumentを作成する必要があります。

var data = XDocument.Parse("<ScoreRule><ShippingMethod Code=\"UPS1DA\"><Destination Country=\"US\" Area=\"IL\" Value=\"0\" /></ShippingMethod></ScoreRule>"); 

      var results = from item in data.Descendants("ShippingMethod") 
          select new 
           { 
            ShippingMethodCode = item.Attribute("Code").Value, 
            Country = item.Element("Destination").Attribute("Country").Value, 
            Area = item.Element("Destination").Attribute("Area").Value 
           }; 
3

これはあなたが望むものですか?

XElement scoreRuleElement = XElement.Parse("<ScoreRule><ShippingMethod Code=\"UPS1DA\"><Destination Country=\"US\" Area=\"IL\" Value=\"0\" /></ShippingMethod></ScoreRule>"); 

XElement shippingMethodElement = scoreRuleElement.Element("ShippingMethod"); 
string code = shippingMethodElement.Attribute("Code").Value; 
XElement destinationElement = shippingMethodElement.Element("Destination"); 
+0

属性「コード」が常に存在することが確実でない限り、2ステップで値を取得します。 string code = string.Empty; XAttribute codeAttribute = shippingMethodElement.Attribute( "Code"); if(codeAttribute!= null) { code = codeAttribute.Value; } – Erin

関連する問題