C # xmlwriter

Source: Internet
Author: User

Xmlwriter works in the same way as xmlreader, but in the opposite order. Using string connections to quickly create XML documents and XML fragments is very attractive, but we should resist this temptation. XML is the representation of infoset, not angle brackets. If stringbuilder is used to connect strings to create XML, the infoset is reduced to the implementation details of the format. Remember that the XML document is not a string. Xmlwriter also has a setting class xmlwritersettings. This class contains indentation, line feed, encoding, XML consistent level options.
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, );
Writer. writeendelement (); // book
Writer. writeendelement (); // bookstore
Writer. writeenddocument ();
LINQ for XML is not as fast as xmlwriter, but it is still very fast and easy to read.
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 uses the most efficient method of XPath expressions in the data structure in the memory. It implements the xpathnavigable interface. By providing xpathnavigator, it can iterate the underlying XML. Xpathnavigator performs random access to XML. Xpathdocument is read-only and xmldocument can be read and written.
// 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 ));
}
If you want to modify the underlying XML node in the form of xpathnavigator, you should use xmldocument instead of xpathdocument. The Calculation of XPath expressions is slow, but it should be editable. In most cases, read-only xpathdocument should be used as much as possible.
// 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.2 m );
Response. Write (string. Format ("price raised from {0} to {1} <br/> ",
Price,
Node. valueas (typeof (decimal ))));
}
The following is the usage of 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 cocould 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/> ");
}

For the author definition and the books. xml file, refer to http://blog.csdn.net/dz43793/archive/2010/05/06/5564617.aspx.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.