c# - 为 XElement 添加换行符
问题描述
我尝试为 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")
);
我想知道是否有人知道该怎么做?
解决方案
你需要使用一个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);
}
↓</p>
<enfinity
major="6"
minor="1"
family="enfinity"
branch="enterprise"
build="0.0.91" />
推荐阅读
- algorithm - 二叉搜索树,我应该如何旋转这棵树来平衡
- bash - 使用 Curl 数据二进制选项,内存不足
- android - 使用文件浏览器对话窗口访问资产文件夹文件
- typescript - 一种更清洁的方式,如何重构异步调用
- vue.js - 如何在动态加载的数据下动态地在 Vue.js 中进行计算
- python-2.7 - 如何从 purchase.order 中获取产品描述的值
- swift - swift - 在变量名中使用整数 for 循环
- amazon-ec2 - www 和 http 在 AWS 中有效,但不是 https
- selenium - 更改 Mozilla 只读属性
- python - 仿射变换后不同高度的字母