Home Database Mysql Tutorial 基于Visual Studio2010讲解LINQ读出数据库数据生成XML

基于Visual Studio2010讲解LINQ读出数据库数据生成XML

Jun 07, 2016 pm 03:00 PM
linq visual based on explain read

LINQ to XML 是一种启用了 LINQ 的内存 XML 编程接口,使用它,可以在 .NET Framework 编程语言中处理 XML。 LINQ to XML 最重要的优势是它与 Language-Integrated Query (LINQ) 的集成。 由于实现了这一集成,因此,可以对内存 XML 文档编写查询,以检索元

LINQ to XML 是一种启用了 LINQ 的内存 XML 编程接口,使用它,可以在 .NET Framework 编程语言中处理 XML。 
LINQ to XML 最重要的优势是它与 Language-Integrated Query (LINQ) 的集成。 由于实现了这一集成,因此,可以对内存 XML 文档编写查询,以检索元素和属性的集合。 LINQ to XML 的查询功能在功能上(尽管不是在语法上)与 XPath 和 XQuery 具有可比性。 Visual C# 2008 集成 LINQ 后,可提供更强的类型化功能、编译时检查和改进的调试器支持。 
通过将查询结果用作 XElement 和 XAttribute 对象构造函数的参数,实现了一种功能强大的创建 XML 树的方法。 这种方法称为“函数构造”,利用这种方法,开发人员可以方便地将 XML 树从一种形状转换为另一种形状。
LINQ to XML 提供了改进的 XML 编程接口,这一点可能与 LINQ to XML 的 LINQ 功能同样重要。 通过 LINQ to XML,对 XML 编程时,您可以实现任何预期的操作,包括:
从文件或流加载 XML。
将 XML 序列化为文件或流。
使用函数构造从头开始创建 XML。
使用类似 XPath 的轴查询 XML。
使用 Add、Remove、ReplaceWith 和 SetValue 等方法对内存 XML 树进行操作。
使用 XSD 验证 XML 树。
使用这些功能的组合,可将 XML 树从一种形状转换为另一种形状。
创建 XML 树是否方便,这一点非常重要。 例如,若要创建一个小型 XML 树,可以编写以下 C# 代码:
XElement contacts = new XElement("Contacts", new XElement("Contact", new XElement("Name", "Patrick Hines"), new XElement("Phone", "206-555-0144", new XAttribute("Type", "Home")), new XElement("phone", "425-555-0145", new XAttribute("Type", "Work")), new XElement("Address", new XElement("Street1", "123 Main St"), new XElement("City", "Mercer Island"), new XElement("State", "WA"), new XElement("Postal", "68042") ) ) ); 
请注意,缩进用于构造 XML 树的代码可显示基础 XML 的结构。 
直接使用 XML 元素
在使用 XML 编程时,主要关注的通常是 XML 元素,也可能关注属性。 在 LINQ to XML 中,可以直接使用 XML 元素和属性。 例如,可以执行以下操作:
创建 XML 元素而根本不使用文档对象。 当必须使用 XML 树的片段时,这可简化编程。
直接从 XML 文件加载 T:System.Xml.Linq.XElement 对象。
将 T:System.Xml.Linq.XElement 对象序列化为文件或流。
使用 LINQ to XML 时,仅在文档的根级别添加注释或处理说明时,才需使用 XDocument  
名称和命名空间的简化处理
处理名称、命名空间和命名空间前缀通常是 XML 编程的复杂部分。LINQ to XML 完全不需要处理命名空间前缀,从而简化了名称和命名空间。 可以轻松控制命名空间前缀。 但如果您决定不显式控制命名空间前缀,则在序列化时,LINQ to XML 将会分配命名空间前缀(如果需要)或使用默认命名空间进行序列化。 如果使用默认命名空间,则生成的文档中将没有命名空间前缀。 
下面我们来看LINQ读出数据库数据生成XML实战例程:

本实例是一个从数据库里读出来省份,城市,地区的数据生成3个XML文件.

首先创建一个Linq to SQL的类, 然后将省份,城市,地区的表拖入类中:

基于Visual Studio2010讲解LINQ读出数据库数据生成XML

然后新建一个类,加入代码如下:

最后执行过代码后可以看到XML效果:

(以城市为例)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Does Windows 11 provide support for VB6 applications? Does Windows 11 provide support for VB6 applications? May 30, 2023 am 08:31 AM

Windows 11 introduces more macOS-style UI elements. You get a redesigned taskbar with the Start menu in the center. Yes, you can change its position, but by default it will be placed in the center. The Control Center has also received some design tweaks. In the next update, support for Android apps will also come along with redesigned Notepad and Media Player. Although all these features make Windows 11 a nice upgrade over Windows 10, certain doubts are brewing in the minds of users. There is a segment of users and developers who are concerned about whether Windows 11 will support legacy applications and technologies, whether they are lagging behind or not. Since V

How to Fix VCRUNTIME140.dll Missing Error on Windows How to Fix VCRUNTIME140.dll Missing Error on Windows May 04, 2023 am 08:04 AM

The VCRUNTIME140.dllismissing error is a problem with your Visual C++ Redistributable file on Windows. You can use this tutorial to solve the problem. Windows applications and software require DLL files to run - without them, they may stop working entirely. For example, if you see a VCRUNTIME140.dllismissing error, this indicates that your PC is missing this file, preventing the application from launching. This may be due to unsuccessful installation of the application. It can even appear after running Windows updates. Thankfully, you can easily

4 Methods to fix CONCRT140.dll not found error 4 Methods to fix CONCRT140.dll not found error Apr 25, 2023 am 09:22 AM

So many users have reported that whenever they try to run the application, it throws an error message stating that code execution cannot continue because CONCRT140.dll was not found. When opening Adobe applications, Halo, ForzaHorizon5, etc., you may encounter CONCRT140.dll not found. So this is not an application-specific issue. Without the correct DLLs installed, applications will not work properly because their code depends on code written in these libraries. In this article, we will learn what CONCRT140.dll is and why it is missing, and how we can download it and fix the error. what is

Fix: Microsoft Visual C++ 2015 Redistributable Setup Failed error 0x80240017 Fix: Microsoft Visual C++ 2015 Redistributable Setup Failed error 0x80240017 Apr 18, 2023 pm 01:07 PM

Microsoft Visual C++ has become an integral part of the Windows operating system required to run most common applications. Now, some users have recently complained about issues they encountered while trying to install Visual C++ Redistributable Packages for 2015 or Microsoft Visual Studio Redistributable Packages for 2013. According to these users, the installer stopped midway and displayed "0x80240017 - Unspecified Error". There could be many reasons behind this failure. So don't do this

Microsoft releases Visual Studio 2022 17.3, here are the highlights Microsoft releases Visual Studio 2022 17.3, here are the highlights Apr 28, 2023 am 10:28 AM

Microsoft has released Visual Studio 202217.3. This update brings support for .NET Multiplatform Application UI (.NETMAUI), the ability to develop applications for Microsoft Teams, C++ improvements, Azure Container Apps, and improved Live Unit Testing. You can see the full list of new features by opening Help > What's New in Visual Studio. The main feature in this update is that .NETMAUI has graduated from preview support and is now available in Visual Studio 2022 on Windows

Macro cannot read USB disk Macro cannot read USB disk Mar 18, 2024 am 11:01 AM

★Fault Phenomenon★: Every time you open the worksheet, it prompts that the macro cannot be found? After opening EXCEL, it closes for no reason and a sending error report appears. The Excel file cannot be opened and prompts an error report to be sent★Solution★: Download macro virus killing software and scan and kill the computer. Special Tips for Macro Virus Killer Software To ensure that this software can detect and remove viruses better, please pay attention to the following points when scanning: 1. Please close or delete 360 ​​first (otherwise, you will be responsible for the problem of deleting data on 360.) 2. This special killer does not support virus scanning and killing of encrypted documents (please remove the password before checking.) 3. Documents set to read-only will cause the cleaning to fail (please remove read-only and check again.) 4. The special killer will check after you Remove viruses from your data files without damaging them. 5. Special Killer will back up before killing viruses.

Visual Studio 2022 for Mac 17.0 comes with native macOS UI and Apple Silicon support Visual Studio 2022 for Mac 17.0 comes with native macOS UI and Apple Silicon support Apr 15, 2023 am 08:04 AM

Microsoft has announced the general release of Visual Studio 2022 for Mac 17.0. It brings huge changes, including a native macOS UI and support for Apple Silicon processors. According to the company, it has also released a new preview version that you can install side-by-side with this version. In older versions of Visual Studio for Mac, Microsoft used various UI techniques that made things look out of place. This update uses

Microsoft releases Developer News extension for Visual Studio after user complaints Microsoft releases Developer News extension for Visual Studio after user complaints Apr 15, 2023 am 10:55 AM

Microsoft has launched a new Developer News extension for Visual Studio so developers can keep up with all the latest software releases and developer activity. This feature used to be on the start page of Visual Studio 2017, but newer versions removed it. To access the news feed after installing the extension, you should see it docked near Solution Explorer and if you click on that tab you will get the news feed. If you don't need the news feed you can go to view and toggle development

See all articles