c# - 使用 Xelement 时如何换行
问题描述
您好,抱歉,如果问题很奇怪,抱歉英语不好。我的代码:
xws.OmitXmlDeclaration = true;
using (XmlWriter xw = XmlWriter.Create(sb, xws))
{
string lb = "\r\n";
XElement I11 = new XElement("I11", lb,
new XElement("I11_TIPAS", "2"), lb,
new XElement("I11_PAV", "pav"), lb,
new XElement("I11_KODAS_IS", "985"), lb,
from iseip in eip
select new XElement("I12", lb,
new XElement("DI12_BAR_KODAS", iseip.DI12_BAR_KODAS), lb,
new XElement("I12_KODAS_SS", iseip.I12_KODAS_SS), lb,
new XElement("I12_KIEKIS", iseip.I12_KIEKIS), lb,
new XElement("I12_FRAKCIJA", iseip.I12_FRAKCIJA), lb), lb
);
I11.Save(xw);
}
System.IO.File.WriteAllText("bbb.eip", sb.ToString());
输出:
需要这样:
问题每个 I12 节点都必须在新行中。问题出在哪里?
解决方案
尝试以下:
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;
List<EIP> eip = new List<EIP>();
using (XmlWriter xw = XmlWriter.Create("bbb.eip", xws))
{
XElement I11 = new XElement("I11", new object[] {
new XElement("I11_TIPAS", "2"),
new XElement("I11_PAV", "pav"),
new XElement("I11_KODAS_IS", "985")
});
foreach(var iseip in eip)
{
XElement I12 = new XElement("I12", new object[] {
new XElement("DI12_BAR_KODAS", iseip.DI12_BAR_KODAS),
new XElement("I12_KODAS_SS", iseip.I12_KODAS_SS),
new XElement("I12_KIEKIS", iseip.I12_KIEKIS),
new XElement("I12_FRAKCIJA", iseip.I12_FRAKCIJA)
});
I11.Add(I12);
}
I11.Save(xw);
}
}
public class EIP
{
public string DI12_BAR_KODAS { get;set;}
public string I12_KODAS_SS { get;set;}
public string I12_KIEKIS { get;set;}
public string I12_FRAKCIJA { get;set;}
}
推荐阅读
- c++ - 求二项式系数的欧拉函数
- python - 如何将两个不同的数组连接成一个数组
- android - 广播接收器将错误数据从一个活动发送到另一个活动
- reactjs - 如果同一页面上有多个 React 根(数百个),是否会出现性能问题?
- spring-boot - oauth2 令牌请求中的附加参数
- vim - 已解决 - 自定义期间的 Vim 编辑器 .vimrc 错误
- html - 我想从自动生成中删除 Wordpress 中的 P 和 Br 标签,但我希望它们在我使用时保留
- excel - 将 Excel 连接到 Outlook
- apache-kafka - 即使在配置广告侦听器后也无法连接到远程 kafka 代理
- constraints - Optaplanner 7 ConstraintStream 根据每个实例的函数值奖励