簡體   English   中英

如何使用 C# 從具有 TextValue 格式元素的 xml 中的多個節點讀取值

[英]How to read values from multiple nodes in xml having element in TextValue format using C#

這是我們正在使用的示例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>

嘗試 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();

            
        }
    }
  
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM