XmlReader读取XML

前端之家收集整理的这篇文章主要介绍了XmlReader读取XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

留着以后用。

StringBuilder output = new StringBuilder();

String xmlString =
    @"<bookstore>
    <book genre='autobiography' publicationdate='1981-03-22' ISBN='1-861003-11-0'>
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
</bookstore>";

// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
    reader.ReadToFollowing("book");

    if (reader.HasAttributes)
    {
        output.Append("Attributes of <" + reader.Name + ">");
        for (int i = 0; i < reader.AttributeCount; i++)
        {
            reader.MoveToAttribute(i);
            output.Append(" " + reader.Name + "=" + reader.Value);
        }
        reader.MoveToElement(); // Moves the reader back to the element node.
    }

}

OutputTextBlock.Text = output.ToString();
原文链接:https://www.f2er.com/xml/300139.html

猜你在找的XML相关文章