我尝试为XElement添加换行符。下面是我需要在文件开头创建的标记
<enfinity
xsi:schemaLocation="http://.../impex
productattributegroup.xsd"
xmlns:xsi="http://...instance"
xmlns="http://...impex"
major="6" minor="1" family="enfinity" branch="enterprise" build="0.0.91">下面是我没有换行符的代码
var rootElement =
new XElement(XMLNS + "enfinity",
new XAttribute(xsi + "schemaLocation", SchemaLocation),
new XAttribute(XNamespace.Xmlns + "xsi", XSI),
new XAttribute("xmlns", XMLNS),
new XAttribute("major", "6"),
new XAttribute("minor", "1"),
new XAttribute("family", "enfinity"),
new XAttribute("branch", "enterprise"),
new XAttribute("build", "0.0.91")
);我想知道有没有人知道怎么做?
发布于 2020-11-09 20:55:09
您需要使用XmlWriter
XmlWriterSettings settings = new XmlWriterSettings()
{
Indent = true,
NewLineOnAttributes = true,
OmitXmlDeclaration = true,
};
using (XmlWriter writer =
XmlWriter.Create(
Console.Out /*substitute with your writer here*/,
settings)
)
{
rootElement.WriteTo(writer);
}↓
<enfinity
major="6"
minor="1"
family="enfinity"
branch="enterprise"
build="0.0.91" />https://stackoverflow.com/questions/64751943
复制相似问题