如何通过在XDocument C#中传递类型来获取节点值

问题描述 投票:0回答:1

我有以下XML。

<subscription>
    <subscription_add_ons type="array">
        <subscription_add_on>
            <add_on_code>premium_support</add_on_code>
            <name>Premium Support</name>
            <quantity type="integer">1</quantity>
            <unit_amount_in_cents type="integer">15000</unit_amount_in_cents>
            <add_on_type>fixed</add_on_type>
            <usage_percentage nil="true"></usage_percentage>
            <measured_unit_id nil="true"></measured_unit_id>
        </subscription_add_on>
    </subscription_add_ons>

我的XMLParse函数

public XNode GetXmlNodes(XElement xml, string elementName)
    {
       List<string> addOnCodes= new List<string>();
       //elementName = "subscription_add_ons ";
        var addOns = xml.DescendantNodes().Where(x => x.Parent.Name == elementName).FirstOrDefault();
        foreach (XNode addOn in addOns)
        {
             //Needed to do something like this
            /*var len =  "add_on_code".Length + 2;
            var sIndex = addOn.ToString().IndexOf("<add_on_code>") + len;
            var eIndex = addOn.ToString().IndexOf("</add_on_code>");
            var addOnCode = addOn.ToString().Substring(sIndex, (eIndex - sIndex)).Trim().ToLower();
            addOnCodes.Add(addOnCode);*/
        }

正如@JonSkeet的评论中提到的,我更新了我的代码片段,如下所示。

  var addOns = xml.Descendants(elementName).Single().Elements();  

  foreach (XNode addOn in addOns)
        {
            /*addon = {<subscription_add_on>
            <add_on_code>premium_support</add_on_code>
            <name>Premium Support</name>
            <quantity type="integer">1</quantity>
            <unit_amount_in_cents type="integer">15000</unit_amount_in_cents>
            <add_on_type>fixed</add_on_type>
            <usage_percentage nil="true"></usage_percentage>
            <measured_unit_id nil="true"></measured_unit_id>
        </subscription_add_on>} */
         //how to get the addOnCode node value ?
            var addOnCode = string.Empty;
            addOnCodes.Add(addOnCode);
        }

但我需要的是从传递的XML中获取subscription_add_on类型的所有节点,然后获取add_on_code中包含的值并将其添加到字符串集合中。

或者通常通过传递类型来获取节点的值?尝试使用来自VS Intellisense的可用方法,但没有得到可以做到这一点的确切方法?

谢谢!

c# xml asp.net-core linq-to-xml
1个回答
0
投票

这是Xml Linq(XDOCUMENT)的解决方案:

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

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

            var results = doc.Descendants("subscription_add_on").Select(x => new
            {
                add_on_code = (string)x.Element("add_on_code"),
                name = (string)x.Element("name"),
                quantity = (int)x.Element("quantity"),
                amount = (int)x.Element("unit_amount_in_cents"),
                add_on_type = (string)x.Element("add_on_type")
            }).ToList();

        }
    }


}
© www.soinside.com 2019 - 2024. All rights reserved.