我有一些XML文件包含节点上方的注释.当我在阅读文件时,作为整个过程的一部分,我也希望得到评论.我知道你可以使用XmlComment写一个评论文件,但不知道如何阅读它们.
我的XML看起来与此类似:
<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>
Run Code Online (Sandbox Code Playgroud)
Has*_*him 16
试试这个:
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreComments = false;
using (XmlReader reader = XmlReader.Create("input.xml", readerSettings))
{
XmlDocument myData = new XmlDocument();
myData.Load(reader);
// etc...
}
Run Code Online (Sandbox Code Playgroud)
阅读评论:
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
Run Code Online (Sandbox Code Playgroud)
Hen*_*man 13
使用System.Xml.Linq:
var doc = XElement.Load(fileName);
var comments = doc.DescendantNodes().OfType<XComment>();
foreach (XComment comment in comments)
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17537 次 |
| 最近记录: |