Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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
如何用c#编写XML?_C#_Xml_Xmltextwriter - Fatal编程技术网

如何用c#编写XML?

如何用c#编写XML?,c#,xml,xmltextwriter,C#,Xml,Xmltextwriter,thx尝试使用XElementendXAttribute类。它们是LINQtoXML的一部分,使XML的使用更加容易 myXmlTextWriter.WriteStartElement("Account");..... myXmlTextWriter.WriteElementString("Accounting",...... 它在.ToString()上返回: 按照模式填充其余属性,您将得到所需的内容。您将需要发出WriteAttributeString: var xml = new X

thx

尝试使用
XElement
end
XAttribute
类。它们是LINQtoXML的一部分,使XML的使用更加容易

myXmlTextWriter.WriteStartElement("Account");.....
myXmlTextWriter.WriteElementString("Accounting",......
它在
.ToString()上返回:



按照模式填充其余属性,您将得到所需的内容。

您将需要发出
WriteAttributeString

var xml = new XElement("Account",
              new XAttribute("nr", 401),
              new XAttribute("name", "Wasser/Abwasser"),
              new XElement("Accounting",
                  new XAttribute("date", "15.02."),
                  new XAttribute("refNr", "...")));
<Account nr="401" name="Wasser/Abwasser">
  <Accounting date="15.02." refNr="..." />
</Account>
并在
writeStarteElement
之后立即执行此操作

您可能也可以使用此重载:

myXmlTextWriter.WriteAttributeString(null, "nr", null, "401");
myXmlTextWriter.WriteEndElement();

当然,对于所有其他属性,都要重复这一点。它对子节点也会起同样的作用。

使用LINQ to XML,您可以非常简单地做到:

myXmlTextWriter.WriteAttributeString("nr", "401");

您是否应该使用XmlTextWriter?LINQ to XML更易于使用您是否特别想使用XmlTextWriter?可以使用LINQ转换为XML吗?(这会让你的生活更轻松…)我不知道WriteAttributeString..我也会尝试你的方法..太好了..谢谢大家..对不起,我只能接受一个正确的答案
myXmlTextWriter.WriteAttributeString("nr", "401");
var document = new XDocument( 
                   new XElement("Account",
                       new XAttribute("nr", 401),
                          ...));
document.WriteTo(myXmlTextWriter);