如何忽略 XML 文件中的前导空格?

发布于 2024-11-04 23:52:29 字数 563 浏览 4 评论 0原文

我需要将 xml 从文件加载到 XmlDocument 中。问题是该文件包含一些前导空格。 (我无法控制生成该文件的系统。) 有没有任何干净/简单的方法来忽略或删除这些字符?

string SamplelRequestFile = @"C:\example.xml";
XmlDocument docXML = new XmlDocument(); 
XmlTextReader xReader = new XmlTextReader(SamplelRequestFile);
XmlReaderSettings ReaderSettings = new XmlReaderSettings();
ReaderSettings.XmlResolver = null;
ReaderSettings.ProhibitDtd = false;
docXML.Load(xReader);

example.xml(注意前导空格)

  <?xml version="1.0" ?>
<myRoot>
<someElement />
</myRoot>

I need to load xml from a file into an XmlDocument. The problem is that the file contains some leading whitespace. (I have no control over the system that produces the file.)
Is there any clean/easy way to ignore or strip those characters?

string SamplelRequestFile = @"C:\example.xml";
XmlDocument docXML = new XmlDocument(); 
XmlTextReader xReader = new XmlTextReader(SamplelRequestFile);
XmlReaderSettings ReaderSettings = new XmlReaderSettings();
ReaderSettings.XmlResolver = null;
ReaderSettings.ProhibitDtd = false;
docXML.Load(xReader);

example.xml (note the leading spaces)

  <?xml version="1.0" ?>
<myRoot>
<someElement />
</myRoot>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

独﹏钓一江月 2024-11-11 23:52:29

你只需要做类似的事情

 using (StreamReader sr = new StreamReader(@"C:\example.xml"))
 {
      XmlDocument docXML = new XmlDocument(); 
      docXML.LoadXml(sr.ReadToEnd().Trim());
      ...
 }

You'll just have to do something like

 using (StreamReader sr = new StreamReader(@"C:\example.xml"))
 {
      XmlDocument docXML = new XmlDocument(); 
      docXML.LoadXml(sr.ReadToEnd().Trim());
      ...
 }
感情废物 2024-11-11 23:52:29

这是一个有效的示例:

        string file = @"C:\example.xml";
        XmlDocument docXML = new XmlDocument();
        using (TextReader x = new StreamReader(file))
        {
            while (x.Peek() == ' ')
                x.Read();
            docXML.Load(x);
        }

here is a sample that works:

        string file = @"C:\example.xml";
        XmlDocument docXML = new XmlDocument();
        using (TextReader x = new StreamReader(file))
        {
            while (x.Peek() == ' ')
                x.Read();
            docXML.Load(x);
        }
揽月 2024-11-11 23:52:29

这是无效的 XML

根据 XML 规范,pi处理指令 必须是第一个字符(如果存在)。

我建议您通过修剪 XML 来预处理 XML。


解决方法:

string content = File.ReadAllText(@"C:\example.xml");
XmlDocument doc = new XmlDocument();
doc.LoadXml(content.Trim());

This is an invalid XML.

According to XML Specification, pi or processing-instructions must be the first characters if they are present.

I suggest you pre-process the XML by trimming the XML.


Workaround:

string content = File.ReadAllText(@"C:\example.xml");
XmlDocument doc = new XmlDocument();
doc.LoadXml(content.Trim());
街道布景 2024-11-11 23:52:29

自己在文件上创建一个 Stream 和一个 StreamReader,然后使用 Peek() 并在看到空格时使用流中的字符。确定下一个字符是 < 后,将流传递给 XmlTextReader 构造函数。

Create a Stream and a StreamReader on the file yourself, then Peek() and consume characters from the stream as long as you see whitespace. Once you're sure that the next character is <, pass the stream to the XmlTextReader constructor.

空心↖ 2024-11-11 23:52:29

您是否尝试过添加此标志?

ReaderSettings.IgnoreWhitespace = true;

Have you tried adding this flag ?

ReaderSettings.IgnoreWhitespace = true;
南巷近海 2024-11-11 23:52:29
string newXml = string.TrimLeft(oldXml);
string newXml = string.TrimLeft(oldXml);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文