How to ignore leading whitespace in XML file?
I need to load xml from a file into an XmlDocument. The problem is that the file contains some leading whitespace. (I ha开发者_如何学Pythonve 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>
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());
...
}
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);
}
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());
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.
Have you tried adding this flag ?
ReaderSettings.IgnoreWhitespace = true;
string newXml = string.TrimLeft(oldXml);
精彩评论