C#获取ATTLIST元素名称

时间:2016-06-28 20:11:13

标签: c# xml dtd xml-dtd

根据W3Schools,attribute的XML具有以下sintax:

<!ATTLIST element-name attribute-name attribute-type attribute-value>

所以在C#上,我可以使用以下code读取任何XML的所有元素:

    XmlReaderSettings readSettings = new XmlReaderSettings();
    readSettings.IgnoreWhitespace = true;
    readSettings.DtdProcessing = DtdProcessing.Parse;
    using (XmlReader reader = XmlReader.Create(@"d:\temp\xml-sign.xml", readSettings))
    {
        while (reader.Read())
        {
            Console.Write(reader.NodeType.ToString().PadRight(20, '-'));
            Console.Write("> ".PadRight(reader.Depth * 5));
            Console.WriteLine(string.Format("Name = {0}, Value = {1}", reader.Name, reader.Value));
        }
    }

作为xml文件和输出的示例:

XML文件

<?xml version="1.0" encoding="utf-8" ?>
<!-- This is a comment -->

<!DOCTYPE Test [<!ATTLIST Book Id ID #IMPLIED>]>         
    <Book Id="ABC">
        <Author>Yazan Diranieh</Author>
        <ISBN>12345</ISBN>
        <Publisher>Publisher 1</Publisher>
    </Book>

输出

XmlDeclaration------> Name = xml, Value = version="1.0" encoding="utf-8"
Comment-------------> Name = , Value =  This is a comment
DocumentType--------> Name = Test, Value = <!ATTLIST Book Id ID #IMPLIED>
Element-------------> Name = Book, Value =
Element------------->    Name = Author, Value =
Text---------------->         Name = , Value = Yazan Diranieh
EndElement---------->    Name = Author, Value =
Element------------->    Name = ISBN, Value =
Text---------------->         Name = , Value = 12345
EndElement---------->    Name = ISBN, Value =
Element------------->    Name = Publisher, Value =
Text---------------->         Name = , Value = Publisher 1
EndElement---------->    Name = Publisher, Value =
EndElement----------> Name = Book, Value =

在下面的一行中,我有我需要的信息,属性:
DocumentType--------> Name = Test, Value = <!ATTLIST Book Id ID #IMPLIED>

我需要获取属性的元素名称,在本例中为“ Book

有同样的方法吗?或者我应该在此值中使用一些搜索字符串吗?

0 个答案:

没有答案