C# XmlWriter

XmlWriter的工作方式类似于XmlReader,但顺序相反。使用字符串连接来快速创建Xml文档和xml片段是非常吸引人的,但我们应抵制这种诱惑。Xml是InfoSet的表示,不是尖括号。如果适用StringBuilder把字符串字面连接在一起来创建xml,就把InfoSet降低为格式实现细节。记住xml文档不是字符串。XmlWriter还有一个设置类XmlWriterSettings。这个类包含缩进、换行、编码、xml一致级别的选项。
 Double price = 49.99;
            DateTime publicationdate = new DateTime(2005, 1, 1);
            String isbn = "1-057-610-0";
            Author a = new Author();
            a.FirstName = "Scott";
            a.LastName = "Hanselman";

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.NewLineOnAttributes = true;

            Response.ContentType = "text/xml";

            XmlSerializerFactory factory = new XmlSerializerFactory();

            using (XmlWriter writer =XmlWriter.Create(Response.OutputStream, settings))
            {
                //Note the artificial, but useful, indenting
                writer.WriteStartDocument();
                writer.WriteStartElement("bookstore");
                writer.WriteStartElement("book");
                writer.WriteStartAttribute("publicationdate");
                writer.WriteValue(publicationdate);
                writer.WriteEndAttribute();
                writer.WriteStartAttribute("ISBN");
                writer.WriteValue(isbn);
                writer.WriteEndAttribute();
                writer.WriteElementString("title", "ASP.NET 2.0");
                writer.WriteStartElement("price");
                writer.WriteValue(price);
                writer.WriteEndElement(); //price
                XmlSerializer xs = factory.CreateSerializer(typeof(Author));
                xs.Serialize(writer, a);
                writer.WriteEndElement(); //book
                writer.WriteEndElement(); //bookstore
                writer.WriteEndDocument();
Linq for xml它不如XmlWriter快,但仍非常快,且非常容易阅读。
  Double price = 49.99;
            DateTime publicationdate = new DateTime(2005, 1, 1);
            String isbn = "1-057-610-0";
            Author a = new Author();
            a.FirstName = "Scott";
            a.LastName = "Hanselman";

            Response.ContentType = "text/xml";
            XNamespace ns = "http://example.books.com";
            XDocument books = new XDocument(
                new XElement(ns + "bookstore",
                    new XElement(ns + "book",
                        new XAttribute("publicationdate", publicationdate),
                        new XAttribute("ISBN", isbn),
                        new XElement(ns + "title", "ASP.NET 2.0 Book"),
                        new XElement(ns + "price", price),
                        new XElement(ns + "author",
                            new XElement(ns + "first-name", a.FirstName),
                            new XElement(ns + "last-name", a.LastName)
                            )
                        )
                   )
                );
            Response.Write(books);
XPathDocument在内存中的数据结构上使用Xpath表达式的最高效方式,它实现了XPathNavigable接口,通过提供XPathNavigator,可以迭代底层的xml。XPathNavigator对xml进行随机访问。XPathDocument是只读的,XmlDocument允许读写。
  //Load document
            string booksFile = Server.MapPath("books.xml");

            XPathDocument document = new XPathDocument(booksFile);
            XPathNavigator nav = document.CreateNavigator();

            //Add a namespace prefix that can be used in the XPath expression
            XmlNamespaceManager namespaceMgr = new XmlNamespaceManager(nav.NameTable);
            namespaceMgr.AddNamespace("b", "http://example.books.com");

            //All books whose price is not greater than 10.00
            foreach (XPathNavigator node in
                nav.Select("//b:book[not(b:price[. > 10.00])]/b:price",
                namespaceMgr))
            {
                Decimal price = (decimal)node.ValueAs(typeof(decimal));
                Response.Write(String.Format("Price is {0}<BR/>",
                    price));
            }
如果要以XPathNavigator的形式修改底层的xml节点,就应该使用XmlDocument代替XPathDocument。Xpath表达式的而计算比较慢,但应该可以编辑。在大多数情况下,应尽多的使用只读的XPathDocument。
 //Load document
        string booksFile = Server.MapPath("books.xml");

        XmlDocument document = new XmlDocument();
        document.Load(booksFile);
        XPathNavigator nav = document.CreateNavigator();

        //Add a namespace prefix that can be used in the XPath expression
        XmlNamespaceManager namespaceMgr = new XmlNamespaceManager(nav.NameTable);
        namespaceMgr.AddNamespace("b", "http://example.books.com");

        //All books whose price is not greater than 10.00
        foreach (XPathNavigator node in
            nav.Select("//b:book[not(b:price[. > 10.00])]/b:price",
            namespaceMgr))
        {
            Decimal price = (decimal)node.ValueAs(typeof(decimal));
            node.SetTypedValue(price * 1.2M);
            Response.Write(String.Format("Price raised from {0} to {1}<BR/>",
                price,
                node.ValueAs(typeof(decimal))));
        }
以下是Linq的而用法
  //Load document
        string booksFile = Server.MapPath("books.xml");
        XDocument document = XDocument.Load(booksFile);

        //Add a namespace prefix that can be used in the XPath expression.
        // Note the need for a NameTable. It could be new or come from elsewhere.
        XmlNamespaceManager namespaceMgr = new XmlNamespaceManager(new NameTable());
        namespaceMgr.AddNamespace("b", "http://example.books.com");

        var nodes = document.XPathSelectElements("//b:book[not(b:price[. > 10.00])]/b:price",namespaceMgr);

        //All books whose price is not greater than 10.00
        foreach (var node in nodes)
        {
            Response.Write(node.Value + "<BR/>");
        }

关于Author的定义和books.xml文件可以在http://blog.csdn.net/dz45693/archive/2010/05/06/5564617.aspx找到

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值