Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of linq to xml Operation XML

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "sample Analysis of linq to xml Operation XML". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The System.Xml.Linq namespace in .net provides support for linq to xml. XDocument,XElement and XText,XAttribute in this namespace provide key methods for reading and writing xml documents.

1. Use linq to xml to write xml:

The constructor of XDocument can be used to construct an Xml document object; the XElement object can be used to construct a xml node element; the XAttribute constructor can be used to construct the attributes of the element; and the XText constructor can be used to construct text within the node.

The following example code:

The copy code is as follows:

Class Program

{

Static void Main (string [] args)

{

Var xDoc = new XDocument (new XElement ("root")

New XElement ("dog"

New XText ("dog said black is a beautify color")

New XAttribute ("color", "black"))

New XElement ("cat")

New XElement ("pig", "pig is great")

/ / xDoc output encoding of xml is the default code of the system, and gb2312 for simplified Chinese operating system

/ / default is indented formatted xml without formatting

XDoc.Save (Console.Out)

Console.Read ()

}

}

The above code will output the following Xml:

The copy code is as follows:

Dog said black is a beautify color

Pig is great

You can see that linq to xml is much more convenient than XmlDocument and XmlWriter.

two。 Use linq to xml to read xml

Linq queries objects from collections, which are obtained through Elements (), Elements (string name) of XElement, and several overloaded methods of Descendants, DescendantsAndSelf, Ancestors, and AncestorsAndSelf.

After obtaining the XElement collection, the attribute value of the element can be obtained through the Attribute (string name) method of XElement, and the text value of the node can be obtained through the Value attribute of XElement. Using linq, you can easily do query and filter sorting.

Still the xml in the example above, we need to read all the word nodes of root and print them out, as shown in the following code:

The copy code is as follows:

Class Program

{

Static void Main (string [] args)

{

Var xDoc = new XDocument (new XElement ("root")

New XElement ("dog"

New XText ("dog said black is a beautify color")

New XAttribute ("color", "black"))

New XElement ("cat")

New XElement ("pig", "pig is great")

/ / xDoc output encoding of xml is the default code of the system, and gb2312 for simplified Chinese operating system

/ / default is indented formatted xml without formatting

XDoc.Save (Console.Out)

Console.WriteLine ()

Var query = from item in xDoc.Element ("root") .Elements

Select new

{

TypeName = item.Name

Saying = item.Value

Color = item.Attribute ("color") = = null? (string) null:item.Attribute ("color") .Value

}

Foreach (var item in query)

{

Console.WriteLine ("{0}'s color is {1}, {0} said {2}", item.TypeName,item.Color?? "Unknown", item.Saying?? "nothing")

}

Console.Read ()

}

}

3. Simple application of Linq to xml

Application requirements: read the rss of the blog park, and then output the latest 10 blog messages on the page

Key points: load Xml through XDocument's Load static method, and query the latest 10 pieces of data through linq

The code is as follows:

The copy code is as follows:

Protected override void OnLoad (EventArgs e)

{

/ / practical application, generate Html code by reading the RSS of the blog park to display the latest blog list

/ / load Xml using the Load static method of XDocument

Var rssXDoc = XDocument.Load ("https://www.jb51.net");

/ / use linq to xml to query the first 10 new blogs

Var queryBlogs = (from blog in rssXDoc.Descendants ("item")

Select new

{

Title = blog.Element ("title") .Value

Url = blog.Element ("link") .Value

PostTime = DateTime.Parse (blog.Element ("pubDate") .Value)

}) .Take (20)

RepeaterBlogs.DataSource = queryBlogs

RepeaterBlogs.DataBind ()

Base.OnLoad (e)

}

Linq to Xml instance

This is the end of the content of "sample Analysis of linq to xml Operation XML". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report