XmlReader and XElement combination to read large xml documents, xmlreaderxelementIntroduction
There are a large number of class libraries and APIs for operating xml data in. NET framework. However, after. NET framework 3.5, we generally prefer to use linq to xml.
Whether it is XElement. the Load method is still XElement. the Parse method loads the entire xml file
Brief introductionThere are a lot of class libraries and APIs in the. NET Framework that manipulate XML data, but after the. NET Framework 3.5 Our preference is generally LINQ to XML.The LINQ to XML operation XML data, either the Xelement.load method or the Xelement.parse method, loads the entire XML file into memory, and LINQ to XML is less appropriate in the case of an XML file that is super large.The best way for a large XML file is to read only one part at a time, so that the entire XML file
0. Preparation: Define products. xml:
1. LINQ to XML1.1 load XML files
Use the load method of xdocument and xelement to read XML files and perform initial transformation of xdocument and xelement.
Xelement ELEM = xelement. load (@ "D:/visualstudioproject2008/test/linq2xml01/products. XML ");
1.2 select processing
A
1. Custom business class to xelement type
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
Xmlserializer serializer
=
New
Xmlserializer (
Typeof
(Cfedataanalysis ));System. Io. Stream writer
=
New
System. Io. memorystream ();Serializer. serialize (writer, analysis );
Byte
[] Result
=
New
Byte
[Writer. Length];Writer. Seek (
0
, Seekorigin. Begin );Writer. Re
When loading XML using the load method, you will find the difference between the two:
The following is a brief summary:
Xdocument. Load () load the entire XML file including the root node
Xelement. Load () does not load the XML Root Node
Xelement. Load () Sample Code:
File.WriteAllText("Test.xml", @"
Result:
Querying tree loaded with XElement.Load----
Xdocument. Load () sample code:
File.WriteAllTex
Xelement. the setelementvalue method is very considerate. It will automatically determine whether the element has the child element. If there is an update or an addition, it will be deleted. It is also easy to laugh, xelement. setelementvalue ("lastname", null), which deletes the lastname element.
// We will use this to store a reference to one of the elements in the XML tree.
Output
Before updating ele
Xelement does not support XPath. It must be implemented through extension. The extension class is system. xml. XPath. Extensions. Therefore, you can use the XPath method to filter elements by referencing the system. xml. XPath namespace. Example:
String xml = "
VaR ELEM = xelement. parse (XML );
Response. Write (ELEM. xpathselectelement ("// scorelogschema"). tostring ());
First, the preface
The last essay is mainly aimed at the matching and conversion between attribute attributes and entities of XML. The main content of this essay is to convert the object into corresponding XML and XElement. These 2 essays transform and match XML in different ways, and each match is operated in different angles. This article is mainly for the transformation of object entities, the next chapter focuses on the matching of XML.
Second,
First, LINQ to XML programming Basics1. LINQ to XML classes
The System.Xml.Linq namespace contains 19 classes, and the following table lists their names and their descriptions:
Class
Describe
XAttribute
Represents an XML attribute
Xcdata
Represents a CDATA text node
Xcomment
Represents an XML annotation
XContainer
Abstract base class for all nodes that may have child nodes
Xdeclaration
Represents an
The XElement class is one of the basic classes in LINQtoXML. It represents an XML element. You can use this class to create elements, change element content, add, change, or delete child elements, add attributes to elements, or serialize element content in text format. It can also interoperate with other classes in System. Xml (such as XmlReader, XmlWriter, and compiledtransform. 1. LINQ to XML class
Hide row number? Create XML
public static void C
author3 =doc.CreateElement("author");author3.InnerText="Jim Wooley";XmlElement title =doc.CreateElement("author");title.InnerText ="LINQ in Action";XmlElement book =doc.CreateElement("book");book.AppendChild(author1);book.AppendChild(author2);book.AppendChild(author3);book.AppendChild(title);books.AppendChild(book);doc.AppendChild(books);
Use LINQ toxml to generate XML documents
new XElement("books", new
1. LINQ to XML class
The following code demonstrates how to use LINQ to XML to quickly create an XML file:
public static void CreateDocument(){ string path = @"d:\website"; XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement("Root", "root")); xdoc.Save(path);}
Run this example to get an XML file with the following content:
2. xelement
Overview of the linq to XML class
The purpose of LINQ to XML is to make the XML name as simple as possible.
Xattribute class
Xattribute indicates an XML Attribute.
Xcdata class
Xcdata indicates a CDATA text node.
Xcomment class
Xcomment indicates an XML comment.
Xdeclaration class
Xdeclaration indicates an XML declaration. The XML declaration is used to declare the encoding of the XML version and document. In addition, the XML Declar
ArticleDirectory
Overview of the linq to XML class
LINQ to XML learning-2. Overview of LINQ to XML programming overview of the linq to XML class overview of the xattribute class
Xattribute indicates an XML Attribute. Xcdata class
Xcdata indicates a CDATA text node. Xcomment class
Xcomment indicates an XML comment. Xcontainer class
Xcontainer is an abstract base class applicable to all nodes that may have subnodes. The following classes are derived from the xiner iner class:
, processing instructions, and annotations.If you want the XDocument class to provide special functionality, you only need to create a XDocument object. In many cases, you can use XElement directly. Direct use of XElement is a relatively simple programming model.The XDocument object can have only one child XElement node. This reflects the XML standard, that is, t
Looking for someone else's article, address http://www.cnblogs.com/mingmingruyuedlut/archive/2011/01/27/1946239.html LINQ to XML provides an efficient way to create XML elements, called function constructs. A function construct is the ability to create an XML tree in a single statement.The LINQ to XML programming interface that enables function construction has several important features: The XElement constructor can take more than one type of param
externally.Xdeclaration declaration = new Xdeclaration ("1.0", "UTF-8", "yes");Context. Response.Write (Declaration);namespaces FOR XML filesXNamespace ns = "http://www.google.com/schemas/sitemap/0.84";XElement siteMap = new XElement (ns + "Urlset");String fixedurl = "Http://www.freeflying.com/article";String Wholeurl = String. Empty;Loop out the data and convert it into an XML nodeforeach (var item in Art
This article does not introduce its concept in detail, instead of speaking in the. NET how to manipulate the XML file. To point out, XML is a subset of standard generic Markup Language (SGML) that can be run across platforms and is ideal for web transmissions, in different programming languages (PHP, Java,. NET, etc.) inside are widely used, so the operation of XML, you will have to master a technology.
Let's look at what the XML document looks like, and here's how to illustrate th
Parent
Gets the parent of this XObject XElement
Previousnode
Gets the previous sibling node of this node
Value
Gets or sets the concatenated text content of this element
List of methods:
Method
Description
Add
Adds the specified content to the child of this XContainer
Addafterself
Add the specified content immediately after this
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.