Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将AttributeString添加到';WriteElementString';C#XML中的元素_C#_Xml - Fatal编程技术网

如何将AttributeString添加到';WriteElementString';C#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

我正在尝试创建一个关于XSD的XML。我生成了一个示例XML文件,并在该文件中写入了一个元素
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
            });
        }
    }
}