I need to find ShippingMethod
and the attribute Code
and Destination
from the following piece of XML:
我需要从以下XML片段中找到ShippingMethod和属性Code and Destination:
<ScoreRule>
<ShippingMethod Code="UPS1DA">
<Destination Country="US" Area="IL" Value="0" />
</ShippingMethod>
</ScoreRule>
How do I retrieve that data with Linq to XML?
如何使用Linq to XML检索该数据?
2 个解决方案
#1
2
Here is the link to XML query expression to select it.
以下是XML查询表达式的链接以选择它。
I didn't know how you were loading your initial data, so I just parsed it in to a document, but you should create your XDocument according to how you are getting your data.
我不知道你是如何加载你的初始数据的,所以我只是将它解析为一个文档,但你应该根据你获取数据的方式创建你的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
};
#2
3
Is this what you want?
这是你想要的吗?
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");
#1
2
Here is the link to XML query expression to select it.
以下是XML查询表达式的链接以选择它。
I didn't know how you were loading your initial data, so I just parsed it in to a document, but you should create your XDocument according to how you are getting your data.
我不知道你是如何加载你的初始数据的,所以我只是将它解析为一个文档,但你应该根据你获取数据的方式创建你的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
};
#2
3
Is this what you want?
这是你想要的吗?
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");