我正在尝试使用C#读取XML文档,我这样做:
XmlDocument myData = new XmlDocument();
myData.Load("datafile.xml");
无论如何,我有时会在阅读XmlNode.ChildNodes时收到评论。
为了让谁遇到同样的要求,最后我就是这样做的:
/** Validate a file, return a XmlDocument, exclude comments */
private XmlDocument LoadAndValidate( String fileName )
{
// Create XML reader settings
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true; // Exclude comments
settings.ProhibitDtd = false;
settings.ValidationType = ValidationType.DTD; // Validation
// Create reader based on settings
XmlReader reader = XmlReader.Create(fileName, settings);
try {
// Will throw exception if document is invalid
XmlDocument document = new XmlDocument();
document.Load(reader);
return document;
} catch (XmlSchemaException) {
return null;
}
}
谢谢你 托马索
答案 0 :(得分:38)
您可以使用XmlReader
XmlReaderSettings.IgnoreComments
设置为true:
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = true;
using (XmlReader reader = XmlReader.Create("input.xml", readerSettings))
{
XmlDocument myData = new XmlDocument();
myData.Load(reader);
// etc...
}
(通过搜索here来自XmlDocument ignore comments
)
答案 1 :(得分:16)
foreach(XmlNode node in nodeList)
if(node.NodeType != XmlNodeType.Comment)
...
答案 2 :(得分:5)
您只需在ChildNodes上添加过滤器即可。 E.g。
var children = myNode.ChildNodes.Cast<XmlNode>().Where(n => n.NodeType != XmlNodeType.Comment);
或者,您可以使用XmlReaderSettings.IgnoreComments为真的设置加载传入XmlReader的XmlDocument。
using (var file = File.OpenRead("datafile.xml"))
{
var settings = new XmlReaderSettings() { IgnoreComments = true, IgnoreWhitespace = true };
using (var xmlReader = XmlReader.Create(file, settings))
{
var document = new XmlDocument();
document.Load(xmlReader);
// Process document nodes...
}
}
答案 3 :(得分:4)
使用XmlReaderSettings
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = true;
XmlReader reader = XmlReader.Create(sourceFilePath, readerSettings);
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(reader);
答案 4 :(得分:2)
Dim pattern As String = String.Empty
Dim xDoc As XmlDocument = New XmlDocument()
xDoc.Load(path)
''Pattern of comments
pattern = "(<!--.*?--\>)"
xDoc.InnerXml = Regex.Replace(xDoc.InnerXml, pattern, String.Empty, RegexOptions.Singleline)
<!--aftr this run ur code-->
答案 5 :(得分:2)
如果要使用XmlDocument而不是XmlReader,最好按名称或使用XPath引用子节点。
然后您不必担心已添加的评论或其他节点,或订单已更改。
XmlDocument myData = new XmlDocument();
myData.Load("datafile.xml");
XmlNode DocNode = myData.DocumentElement;
XmlNode Child = DocNode ["SomeChildNode"];
这将选择“SomeChildNode”,它是根元素的子元素。
下一个示例将遍历books.xml中的所有书籍并打印作者。它使用字符串属性选择器和Xpath。它不应受评论等影响。
XmlDocument myData = new XmlDocument();
myData.Load("books.xml");
XmlNode DocNode = myData.DocumentElement;
XmlNodeList BookNodeList = DocNode.SelectNodes("./book");
foreach (XmlNode Book in BookNodeList)
{
Console.WriteLine(Book["author"].InnerText);
}
注意,使用XPath,您可以使用“.// book”之类的内容轻松搜索文档中的所有书籍元素。
的books.xml:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<catalog>
参考文献:
XmlNode.Item属性(字符串)hxxp://msdn.microsoft.com/en-us/library/sss31aas.aspx XmlNode.SelectNodes方法(字符串)http://msdn.microsoft.com/en-us/library/hcebdtae.aspx XmlNode.SelectSingleNode方法(字符串)http://msdn.microsoft.com/en-us/library/fb63z0tw.aspx