【问题标题】:How to get the node value by passing type in XDocument C#如何通过在 XDocument C# 中传递类型来获取节点值
【发布时间】:2019-04-05 07:12:48
【问题描述】:

我有以下 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 在 cmets 中提到的,我将我的 sn-p 更新如下。

  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 的可用方法,但没有得到可以做到这一点的确切方法?

谢谢!

【问题讨论】:

  • 您能具体说明一下现在发生了什么吗? (注释掉的代码肯定可以改进,但我假设您是从尝试获取正确的元素开始的。)我希望找到&lt;subscription_add_ons&gt; 元素,然后只找到子元素,例如var addOns = xml.Descendants(elementName).Single().Elements();
  • @JonSkeet,谢谢,在上面的 XML 中,您能否向我解释一下 Node & Element 是什么?因为我用你的 sn-ps 尝试过,它在我的返回节点时重新调整元素?
  • 元素是节点,是一种更具体的节点。 (例如,它们不是属性或文本节点。)这使它们更易于使用,因为XElement 上的功能比XNode 上的功能更多。如果您对 XML 非常陌生,我建议您在开始尝试在代码中使用它们之前阅读 XML 入门。
  • @JonSkeet。谢谢!谷歌搜索给了我下面的pdf。我肯定会检查这个w3c.it/education/2012/upra/documents/xmlprimer.pdf
  • @JonSkeet,请您查看帖子中更新的 sn-p。并建议我现在如何找到节点值?谢谢!

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


【解决方案1】:

这是使用 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();

        }
    }


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多