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

时间:2019-04-05 07:12:48

标签: c# xml asp.net-core linq-to-xml

我下面有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中包含的值并将其添加到字符串集合中。

或者通常通过传递类型来获取node的值?尝试过VS Intellisense提供的可用方法,但没有获得可以执行此操作的确切方法?

谢谢!

1 个答案:

答案 0 :(得分: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();

        }
    }


}