我想创建一个XML文件。但现在我的问题是创建以下XML属性:
<MsrProgram OwnerTypeFullName="myData" Number="0">
<MsrRange2Width>4</MsrRange2Width>
</MsrProgram>
我的问题是如何创建以下属性: OwnerTypeFullName = “myData的” 号= “0”
这是我的XML类:
public class MsrProgram
{
/// <summary>
/// Gets or sets the MsrRange2Width.
/// </summary>
/// <remarks>
/// Value stored in ....
/// </remarks>
[XmlElementAttribute(ElementName = "MsrRange2Width")]
public string MsrRange2Width { get; set; }
}
谢谢!
答案 0 :(得分:1)
使用Xml Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication16
{
class Program
{
static void Main(string[] args)
{
XElement msrProgram = new XElement("MsrProgram", new object[] {
new XAttribute("OwnerTypeFullName","myData"),
new XAttribute("Number", 0),
new XElement("MsrRange2Width",4)
});
}
}
}