本文介绍了阅读在C#XML注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有包含节点的上述评论的一些XML文件。当我读的文件,作为这一进程的一部分我想获得的评论出来为好。我知道你可以使用的 XMLCOMMENT 的写入该文件的评论,但我不知道如何读他们退了出去。
I have got some XML files that contain comments above the nodes. When i am reading the file in, as part of the process i would like to get the comment out as well. I know you can write a comment to the file using XmlComment, but i'm not sure how to read them back out.
我的XML看起来类似于这样:
My XML looks similar to this:
<Objects>
<!--Comment about node-->
<GUID-bf2401c0-ef5e-4d20-9d20-a2451a199362>
<info job="SAVE" person="Joe" />
<info job="SAVE" person="Sally" />
</GUID-bf2401c0-ef5e-4d20-9d20-a2451a199362>
<!--Another Comment about node-->
<GUID-bf2401c0-ef5e-4d20-9d20-a5844113284112>
<info job="SAVE" person="John" />
<info job="SAVE" person="Julie" />
</GUID-bf2401c0-ef5e-4d20-9d20-a5844113284112>
谢谢
卢克
推荐答案
试试这个:
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = false;
using (XmlReader reader = XmlReader.Create("input.xml", readerSettings))
{
XmlDocument myData = new XmlDocument();
myData.Load(reader);
// etc...
}
要阅读评论:
XmlReader xmlRdr = XmlReader.Create("Test.XML");
// Parse the file
while (xmlRdr.Read())
{
switch (xmlRdr.NodeType)
{
case XmlNodeType.Element:
// You may need to capture the last element to provide a context
// for any comments you come across... so copy xmlRdr.Name, etc.
break;
case XmlNodeType.Comment:
// Do something with xmlRdr.value
这篇关于阅读在C#XML注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!