【问题标题】:Reading Large XML files in C# Returns null when looking for an element在 C# 中读取大型 XML 文件查找元素时返回 null
【发布时间】:2020-02-12 20:01:36
【问题描述】:

我需要读取一个大型 XML 文件,但我尝试过的所有内容都返回 NULL。

public void readXml()
{
  XElement xelement = XElement.Load(DewesoftDevices.xmlFileName);
  IEnumerable<XElement> devices = xelement.Elements("Devices");

  foreach (var device in devices)
  {
     Console.WriteLine(device);
  }
}

这是 xml 文件的一部分:

<?xml version="1.0" encoding="UTF-8"?>
<DataFileSetup>
    <System Name="Local">
        <SysInfo>
         'Stuff in here that I dont need
        </SysInfo>
        <DewesoftSetup>
           <Devices>
               <StartStoreTime>43861.6768385532</StartStoreTime>
               <SampleRate>100</SampleRate>
               <BlockSize>1000</BlockSize>
               <IBRate>10</IBRate>
           </Devices>
        </DewesoftSetup>
    </System>
</DataFileSetup>

我不知道这是否重要,但这些都在文件中。

【问题讨论】:

  • 实际上,我对开头的内容更感兴趣。一个常见的错误是忘记考虑 XML 命名空间 (xmlns)。
  • 请提供minimal reproducible example 并确保小数据样本(类似于已在帖子中的样本)实际证明了问题。特别要确保为您尝试查询的节点提供所有相关的命名空间。
  • 我添加了更多的 XML 代码。
  • XML 文件的大小是多少?
  • 大约 500 kb

标签: c# xml linq-to-xml


【解决方案1】:

请尝试以下方法:

c#

XDocument xdoc = XDocument.Load(DewesoftDevices.xmlFileName)
var devices = xdoc.Descendants("Devices");
foreach (XElement x in devices)
{
    Console.WriteLine(x);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-23
    • 2011-05-14
    • 2015-12-23
    • 1970-01-01
    • 2017-12-03
    • 2023-02-02
    • 2020-07-31
    • 1970-01-01
    相关资源
    最近更新 更多