【问题标题】:How to filter data from xml using xmldocument class & xpath如何使用 xmldocument 类和 xpath 过滤来自 xml 的数据
【发布时间】:2019-08-04 12:53:21
【问题描述】:

我将 xml 存储在字符串变量中。从那个 xml 我需要根据 StandardValue 过滤数据。 我只想提取那些 StandardValue 不为空且不为空的记录。我试过了,但我的代码不起作用。

string xmldoc= @"<?xml version=""1.0"" encoding=""utf-8""?>
<TickerBrokerStandardDateLineitem>
  <Ticker />
  <TickerID />
  <TickerBrokerStandardDateLineitemValues>
    <TickerBrokerStandardDateLineitemValue>
      <TabName>Consensus Model</TabName>
      <StandardDate>1Q 2010</StandardDate>
      <BRTab>Income Statement</BRTab>
      <BRLineItem>NET REVENUES</BRLineItem>
      <Action>Extracted</Action>
      <StandardLineItem>Net Revenue</StandardLineItem>
      <StandardValue>329.623</StandardValue>
    </TickerBrokerStandardDateLineitemValue>
    <TickerBrokerStandardDateLineitemValue>
      <TabName>Consensus Model</TabName>
      <StandardDate>2Q 2010</StandardDate>
      <BRTab>Income Statement</BRTab>
      <BRLineItem>NET REVENUES</BRLineItem>
      <Action>Extracted</Action>
      <StandardLineItem>Net Revenue</StandardLineItem>
      <StandardValue></StandardValue>
    </TickerBrokerStandardDateLineitemValue>
    <TickerBrokerStandardDateLineitemValue>
      <TabName>Consensus Model</TabName>
      <StandardDate>2Q 2010</StandardDate>
      <BRTab>Income Statement</BRTab>
      <BRLineItem>NET REVENUES</BRLineItem>
      <Action>Extracted</Action>
      <StandardLineItem>Net Revenue</StandardLineItem>
      <StandardValue/>
    </TickerBrokerStandardDateLineitemValue>
  </TickerBrokerStandardDateLineitemValues>
</TickerBrokerStandardDateLineitem>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmldoc);
XmlNodeList nodeList = doc.GetElementsByTagName("TickerBrokerStandardDateLineitemValue");
List<string> list = new List<string>();
foreach (XmlNode item in nodeList)
{
    foreach (XmlElement i in item)
    {
        if (i.Name == "StandardValue")
        {
            if (i.InnerText == string.Empty)
            {
                list.Add(item.OuterXml);
            }
        }
    }
}
string a = string.Empty;
foreach (var item in list)
{
    a = doc.InnerXml.Replace(item, "");
}

string str1 = doc.OuterXml;

我上面的代码不起作用。基本上如何使用 xpath 进行过滤,只返回那些 StandardValue 不为空且不为空的记录。

如何用 XmlDocument 类而不是 xdocument 来实现。

最后我必须将过滤记录的 xml 存储到字符串中。我知道 XmlDocument 类具有返回完整 xml 的外部 xml 属性。

给我示例代码,它将过滤器记录和存储的过滤器记录 xml 返回到字符串中。

【问题讨论】:

    标签: c# xml xpath xmldocument


    【解决方案1】:

    使用 xml linq,它是较新版本的 Net xml 库:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                string xml = File.ReadAllText(FILENAME);
                XDocument doc = XDocument.Parse(xml);
    
                List<XElement> tickerBrokerStandardDateLineitemValues = doc.Descendants("TickerBrokerStandardDateLineitemValue")
                    .Where(x => (x.Element("StandardValue") != null) && ((string)x.Element("StandardValue") != string.Empty))
                    .ToList();
            }
        }
    }
    

    【讨论】:

    • 我必须使用 xmldocument 类...如果可能,请使用 xmldocument 发布答案。谢谢
    【解决方案2】:

    假设您在问题中提供的输入字符串。

    这将选择所有具有 StandardValue 元素且不为空或空白 (normalize-space) 的 TickerBrokerStandardDateLineitemValue 值。

    normalize-space:

    从字符串中去除前导和尾随空格,替换 由单个空格组成的空白字符序列,并返回 结果字符串。

    var xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xmlStr); // <input from the question>
    
    var nodes = xmlDoc.SelectNodes("//TickerBrokerStandardDateLineitemValue[StandardValue and string-length(normalize-space(StandardValue))]");
    

    【讨论】:

    • 请告诉我这个StandardValue and string-length(normalize-space(StandardValue))的含义
    • 我更新了关于规范化空间的答案。一般在xPath:StandardValue(select non null)中,标准化值后长度大于0。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2021-01-12
    相关资源
    最近更新 更多