首页 > 其他分享 >[代码]创建XDocument对象(LINQ to XML)

[代码]创建XDocument对象(LINQ to XML)

时间:2023-06-30 19:02:45浏览次数:42  
标签:XML XElement System LINQ XDocument using new


LINQ to XML中的XDocument是用来表示内存中的XML文档的。同样可以使用函数构造功能来创建此对象。
此示例代码主要用来演示如何创建一个通用的XML文档。

示例代码代码中创建的XDocument对象,包含了2个注释(XComment),1个处理指令(XProcessingInstrucation),1个根元素及其若干子元素,也为文档添加了XML声明(XDeclaration)。最将所生成的XML内容打印到控制台,并保存到文件中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Demo05
{
    class Program
    {
        static void Main(string[] args)
        {
            XDocument Document = new XDocument(
                new XComment("This is a comment."),
                new XProcessingInstruction("xml-stylesheet",
                    "href='mystyle.css' title='Compact' type='text/css'"),
                new XElement("Pubs",
                    new XElement("Book",
                        new XElement("Title", "Artifacts of Roman Civilization"),
                        new XElement("Author", "Moreno, Jordao")),
                    new XElement("Book",
                        new XElement("Title", "Midieval Tools and Implements"),
                        new XElement("Author", "Gazit, Inbar"))),
                new XComment("This is another comment."));

            Document.Declaration = new XDeclaration("1.0", "utf-8", "true");

            Console.WriteLine(Document);

            Document.Save(@"C:/LINQ/XDocument.xml");
        }
    }
}


打印到控制台,以及保存到XDocument.xml文件中的XML内容如下:

<?xml version="1.0" encoding="utf-8"?>
<!--This is a comment.-->
<?xml-stylesheet href='mystyle.css' title='Compact' type='text/css'?>
<Pubs>
  <Book>
    <Title>Artifacts of Roman Civilization</Title>
    <Author>Moreno, Jordao</Author>
  </Book>
  <Book>
    <Title>Midieval Tools and Implements</Title>
    <Author>Gazit, Inbar</Author>
  </Book>
</Pubs>
<!--This is another comment.-->


怎么样,是不是挺帅的!^_^

标签:XML,XElement,System,LINQ,XDocument,using,new
From: https://blog.51cto.com/u_16173572/6592911

相关文章

  • [代码]如何从文件加载XML(LINQ to XML)
    此示例代码演示如何使用XElement.Load()方法从文件中加载XML。示例代码usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Xml.Linq;namespaceDemo02Ex01{classProgram{staticvoidMain(string[]arg......
  • [代码]如何分析XML字符串(LINQ to XML)
    此代码演示如何使用XElement.Parse()方法分析XML字符串,并创建XElement元素。从编程效率来讲这种做法可能效率非常的高,但从代码的执行效率来看,还是使用函数构造的方式创建XML树的效率高一点。示例代码usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSys......
  • [代码]元素的附加和克隆(LINQ to XML)
    当将一个XElement对象添加给一个XML树的时候,如果此XElement对象没有父级,即不是另外一个XML树的一部分,此时将会将此元素直接添加给XML树;如果XElement对象有父级,则会克隆这个XElement对象,然后将克隆的对象添加到新的XML树。示例代码代码中的Child1被添加到XmlTree1元素,因此它已经有了......
  • CentOs6 yum源 centos/6.4/os/x86_64/repodata/repomd.xml报错的解决方法
    CentoS6停止维护更新日期2020年11月30日CentOS7停止维护更新日期2024年6月30日CentOS8停止维护更新日期2021年12月31日因为测试需要,安装了CentOS6.4,进行更新时yum提示不可用。按照阿里云开源镜像(https://developer.aliyun.com/mirror/centos)......
  • XML PHP SimpleXMLElement Object数组转化为普通数组
    做微信第三方接口接入的时候发现接口返回的数据都是xml格式的。以下是如何把xml格式转化为普通的数组格式取值。xml格式数据:$xmlstr="<xml><mch_appid>".$data['mch_appid']."</mch_appid><mchid>".$data['mchid']."</mchid>......
  • IDEA创建*.xml文件为灰色的解决办法
    问题:通过flie创建*.xml文件时如下显示,文件内容也不会有高亮显示:解决办法:点击Setting—Editor—FileTypes—Auto-detectfiletypebycontent标签,删掉其中的记录即可效果: 转载:(24条消息)IDEA创建*.xml文件为灰色的解决办法_啊哈ㄚ的博客-CSDN博客 ......
  • xml注释
    把xml代码注释掉:<!--这是一个注释--><TextViewandroid:id="@+id/text_view"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="HelloWorld"/><!--这是多行注......
  • 光脚丫学LINQ(006):投影
     视频演示:http://u.115.com/file/f21042d848 select子句生成查询结果并指定每个返回的元素的“形状”或类型。例如,您可以指定结果包含的是整个Customer对象、仅一个成员、成员的子集,还是某个基于计算或新对象创建的完全不同的结果类型。当select子句生成除源元素副本以外的......
  • 光脚丫学LINQ(003):排序结果集
    视频演示:http://u.115.com/file/f2e2959888 通常可以很方便地将返回的数据进行排序。orderby子句将使返回的序列中的元素按照被排序的类型的默认比较器进行排序。例如,下面的查询可以扩展为按Name属性对结果进行排序。因为Name是一个字符串,所以默认比较器执行从A到Z的字母......
  • 光脚丫学LINQ(014):LINQ to SQL简介
    视频演示:http://u.115.com/file/f29f7838f6 LINQtoSQL是.NETFramework3.5版的一个组件,提供了用于将关系数据作为对象管理的运行时基础结构。说明关系数据显示为由二维表(关系或平面文件)组成的集合,其中公共列将表互相关联起来。若要有效地使用LINQtoSQL,您必须略为熟悉关......