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