0
点赞
收藏
分享

微信扫一扫

[代码]使用函数构造创建具有属性的元素(LINQ to XML)


此代码主要示范如何使用LINQ to XML的函数构造功能创建具有属性的元素。实际上是向XElement的构造函数添加XAttribute实例对象。

示例代码此示例代码创建了一个名为Phone的元素,并且为此元素添加了2个属性:Type和Country。

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

namespace Demo04Ex01
{
    class Program
    {
        static void Main(string[] args)
        {
            XElement Phone = new XElement("Phone",
                "555-555-5555",
                new XAttribute("Type", "Home"),
                new XAttribute("Country", "US"));
            Console.WriteLine(Phone);
        }
    }
}


打印到控制台上的XML内容如下:
<Phone Type="Home" Country="US">555-555-5555</Phone>

举报

相关推荐

0 条评论