如何使用 xmldocument class & xpath 从 xml 过滤数据

How to filter data from xml using xmldocument class & xpath

我有 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 过滤 return 只有那些 StandardValue 不为 null 且不为空 .

的记录

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

最后我必须将过滤记录的 xml 存储到字符串中。我知道 XmlDocument class 有外部 xml 属性 其中 return 完整 xml.

给我示例代码,它将 return 过滤记录和存储的过滤记录 xml 转换为字符串。

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

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

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

normalize-space:

strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string.

var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr); // <input from the question>

var nodes = xmlDoc.SelectNodes("//TickerBrokerStandardDateLineitemValue[StandardValue and string-length(normalize-space(StandardValue))]");