根据兄弟价值选择价值

时间:2017-10-29 13:36:47

标签: c# xml linq xml-parsing

我一直试图找出一种方法,而不使用foreach循环来从xml中选择我需要的值。

这是XML。

<?xml version="1.0"?>
<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Count>116</Count>
<Message>Results returned successfully</Message>
<SearchCriteria>VIN:KM8JM12B66U253804</SearchCriteria>
<Results>
    <DecodedVariable>
        <VariableId>10</VariableId>
        <Variable>Destination Market</Variable>
    </DecodedVariable>
    <DecodedVariable>
        <VariableId>26</VariableId>
        <Variable>Make</Variable>
        <ValueId>498</ValueId>
        <Value>HYUNDAI</Value>
    </DecodedVariable>
    <DecodedVariable>
        <VariableId>27</VariableId>
        <Variable>Manufacturer Name</Variable>
        <ValueId>1034</ValueId>
        <Value>HYUNDAI-KIA AMERICA TECHNICAL CENTER INC (HATCI)</Value>
    </DecodedVariable>
    <DecodedVariable>
        <VariableId>28</VariableId>
        <Variable>Model</Variable>
        <ValueId>2058</ValueId>
        <Value>Tucson</Value>
    </DecodedVariable>
    <DecodedVariable>
        <VariableId>29</VariableId>
        <Variable>Model Year</Variable>
        <ValueId/>
        <Value>2006</Value>
    </DecodedVariable>
</Results>

使用linq使用第二个DecodedVariable节点,如何在HYUNDAI时选择variabileId = 26的值?

1 个答案:

答案 0 :(得分:1)

真正的XML只有一个根元素。您有&#39;回复&#39;&#39;计数&#39;,&#39;消息&#39; SearchCriteria&#39;和&#39;结果&#39;作为根元素。您还必须关闭“响应”#39;标签

创建一个如下所示的结构:

<?xml version="1.0"?>
<Content>
    <Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Count>116</Count>
    ...
</Content>

然后,您可以使用System.Xml.Linq.XDocument.Parse()XDocument.Load()加载XML。

以下是一个示例,了解您正在寻找的价值。

//Load XML
XDocument document = XDocument.Parse("<?xml version=\"1.0\"?><Content><Count>116</Count><Message>Results returned successfully</Message><SearchCriteria>VIN:KM8JM12B66U253804</SearchCriteria><Results>    <DecodedVariable>        <VariableId>10</VariableId>        <Variable>Destination Market</Variable>    </DecodedVariable>    <DecodedVariable>        <VariableId>26</VariableId>        <Variable>Make</Variable>        <ValueId>498</ValueId>        <Value>HYUNDAI</Value>    </DecodedVariable>    <DecodedVariable>        <VariableId>27</VariableId>        <Variable>Manufacturer Name</Variable>        <ValueId>1034</ValueId>        <Value>HYUNDAI-KIA AMERICA TECHNICAL CENTER INC (HATCI)</Value>    </DecodedVariable>    <DecodedVariable>        <VariableId>28</VariableId>        <Variable>Model</Variable>        <ValueId>2058</ValueId>        <Value>Tucson</Value>    </DecodedVariable>    <DecodedVariable>        <VariableId>29</VariableId>        <Variable>Model Year</Variable>        <ValueId/>        <Value>2006</Value>    </DecodedVariable></Results></Content>");
//Select root Element
var root = document?.Root;
//Go down to the 'Results' element. There is just a single element so use 'Element()'
var results = root?.Element("Results");
//Get every 'DecodedVariable'. There are multiple elements so use 'Elements()'
var decodedVariables = results?.Elements("DecodedVariable");
//Select the only element with 'VariableId'==26 or null if no element maches
var value = decodedVariables?.SingleOrDefault((decodedVariable) =>
//Do the following for every element 'DecodedVariable'
{
    //Try parse the 'VariableId' as int
    if (int.TryParse(decodedVariable?.Element("VariableId")?.Value, out int id))
        //Compare the value of 'VariableId' with the value you're looking for. In this case 26
        if (id == 26)
            //This is the correct 'DecodedVariable' element
            return true;
    //This is not the correct 'DecodedVariable' element
    return false;
})
//Get the element 'Value' from the element 'DecodedVariable' then get the value from the element 'Value'
?.Element("Value")?.Value;

//'HYUNDAI' is stored in 'value'
Console.WriteLine(value);

到处都是零。如果XElement.Elements()无法找到匹配的元素,则只返回null。这就是我使用?.的原因。如果事先验证了架构,则可能不需要它。虽然你不这样做,或者你会意识到你的xml无效。

您不必解析变量ID&#39;到int。您还可以将其与== 26.ToString()字符串进行比较。

我确定我忘了在几个地方检查无效。因此,在使用之前请自行检查代码。