如何将AttributeString添加到';WriteElementString';C#XML中的元素
我正在尝试创建一个关于XSD的XML。我生成了一个示例XML文件,并在该文件中写入了一个元素如何将AttributeString添加到';WriteElementString';C#XML中的元素,c#,xml,C#,Xml,我正在尝试创建一个关于XSD的XML。我生成了一个示例XML文件,并在该文件中写入了一个元素2560602000 我试图用C#代码生成这行代码,我的代码是 writer.WriteStartElement("test", null, "2560602000"); writer.WriteAttributeString("tip", "abc"); writer.WriteEndElement(); 以上代码正在生成 如果我将代码更改为writer.WriteAttributeStri
2560602000
我试图用C#代码生成这行代码,我的代码是
writer.WriteStartElement("test", null, "2560602000");
writer.WriteAttributeString("tip", "abc");
writer.WriteEndElement();
以上代码正在生成
如果我将代码更改为writer.WriteAttributeString(“tip”,“abc”);
WriteElementString(“test”,null,”)代码>
这是一个错误
我的问题是,如何生成我上面解释的行
已解决
我用这个解决方案解决了我的问题
:
易于使用Xml Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XElement test = new XElement("test", new object[] {
new XAttribute("tip", "abc"),
2560602000
});
}
}
}
如何将其与writer元素一起使用?我应该把这行代码写到我的主xml中。你需要将元素添加到主xml中。如有必要,可以使用test.ToString()将元素转换为字符串。我不知道主xml是什么样子,所以我不能给出更好的答案。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XElement test = new XElement("test", new object[] {
new XAttribute("tip", "abc"),
2560602000
});
}
}
}