Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用C从包含TextValue格式元素的xml中的多个节点读取值#_C# - Fatal编程技术网

C# 如何使用C从包含TextValue格式元素的xml中的多个节点读取值#

C# 如何使用C从包含TextValue格式元素的xml中的多个节点读取值#,c#,C#,这是我们正在使用的示例xml,需要读取TextValue属性中的值 <SelectApplicableVehicle> <TextValue> <text>2000 - 57y-ry5465-Truck</text> <value>2000 - 57y-ry5465-Truck-21063</value>

这是我们正在使用的示例xml,需要读取TextValue属性中的值

<SelectApplicableVehicle>         
        <TextValue> 
           
            <text>2000 - 57y-ry5465-Truck</text>
            <value>2000 - 57y-ry5465-Truck-21063</value>
        </TextValue>
        <TextValue>
            <text>2008 - 57y-3546-Truck</text>
            <value>2008 - 57y-3546-Truck-21064</value>
        </TextValue>
    </SelectApplicableVehicle>

2000-57y-ry5465-卡车
2000-57y-ry5465-Truck-21063
2008-57y-3546-卡车
2008-57y-3546-Truck-21064
试试xml linq:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
         static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("TextValue").Select(x => new
            {
                key = (string)x.Element("text"),
                value = (string)x.Element("value")
            }).ToList();

            
        }
    }
  
}

多了解一点就好了。你已经试过什么了。什么不起作用。在上面的XML中,TextValue是一个元素,而不是一个属性!