本笔记摘抄自:https://www.cnblogs.com/yaozhenfa/p/CSharp_Linq_For_Xml.html,记录一下学习过程以备后续查用。

    一、生成xml

    1.1创建简单的xml

C# LINQ学习笔记五:LINQ to XML
    /// <summary>
    /// CreateXml类
    /// </summary>
    public class CreateXml
    {
        /// <summary>
        /// 返回xml文件路径
        /// </summary>
        public string Path
        {
            get
            {
                string path = @"..\..\LinqToXml.xml";
                return path;
            }
        }

        /// <summary>
        /// 创建简单的xml并保存
        /// </summary>
        public void CreateElement()
        {
            XDocument xdoc = new XDocument
                (
                    //创建一个xml文档
                    //设置该xml的版本为1.0,采用utf - 8编码,后面的yes表示该xml是独立的。
                    new XDeclaration("1.0", "utf-8", "yes"),
                    //开始创建每个节点的,首先是Root节点,然后在Root节点中添加两个Item节点。
                    new XElement
                    (
                        "Root",
                        new XElement("Item", "1"),
                        new XElement("Item", "2")
                    )
                );
            xdoc.Save(Path);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            #region 创建简单的xml
            CreateXml xml = new CreateXml();
            xml.CreateElement();
            #endregion
        }
    }
View Code

相关文章:

  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
  • 2021-08-14
  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-30
  • 2021-09-09
  • 2021-06-15
  • 2022-02-13
  • 2021-09-01
  • 2021-11-09
  • 2021-12-13
相关资源
相似解决方案