如何输出 XmlDocument 以便元素属性也缩进?
我想输出 XmlDocument,以便属性也缩进。 我尝试了两种方法:
var cfgXmlDoc = new XmlDocument();
cfgXmlDoc.PreserveWhitespace = true;
cfgXmlDoc.Load(cfgFilePath);
...
File.WriteAllText(cfgFilePath, cfgXmlDoc.OuterXml);
并且
var cfgXmlDoc = new XmlDocument();
cfgXmlDoc.Load(cfgFilePath);
...
using (var xmlWriter = new XmlTextWriter(cfgFilePath, Encoding.UTF8))
{
xmlWriter.Formatting = Formatting.Indented;
cfgXmlDoc.WriteTo(xmlWriter);
}
正如预期的那样,没有缩进属性。有谁知道该怎么做?
谢谢。
EDIT1
例如,考虑一下 XML 的这一部分:
<dataPortalProxies thisEndpointKind="Agent">
<dataPortalProxy isEnabled="true" name="NC Server" endpointKind="Server"
implementation="Shunra.Common.Csla.WcfOneWayProxy, Shunra.Common">
<add key="AddressTemplate" value="net.msmq://{0}/private/nc_queue"/>
</dataPortalProxy>
<dataPortalProxy isEnabled="true" name="Peer Agent" endpointKind="Agent"
implementation="Shunra.Common.Csla.WcfDynamicProxy, Shunra.Common">
<add key="AddressTemplate" value="https://{0}:7000/NCAgent/WcfPortal.svc"/>
</dataPortalProxy>
</dataPortalProxies>
将其放入 XmlDocument 会产生以下结果:
<dataPortalProxies thisEndpointKind="Agent">
<dataPortalProxy isEnabled="true" name="NC Server" endpointKind="Server" implementation="Shunra.Common.Csla.WcfOneWayProxy, Shunra.Common">
<add key="AddressTemplate" value="net.msmq://{0}/private/nc_queue" />
</dataPortalProxy>
<dataPortalProxy isEnabled="true" name="Peer Agent" endpointKind="Agent" implementation="Shunra.Common.Csla.WcfDynamicProxy, Shunra.Common">
<add key="AddressTemplate" value="https://{0}:7000/NCAgent/WcfPortal.svc" />
</dataPortalProxy>
</dataPortalProxies>
我想要的是属性的某种换行长行,以便任何超过特定行宽的属性都会缩进在下一行。简而言之,漂亮的印刷。
I want to output XmlDocument so that the attributes are indented as well.
I tried two approaches:
var cfgXmlDoc = new XmlDocument();
cfgXmlDoc.PreserveWhitespace = true;
cfgXmlDoc.Load(cfgFilePath);
...
File.WriteAllText(cfgFilePath, cfgXmlDoc.OuterXml);
AND
var cfgXmlDoc = new XmlDocument();
cfgXmlDoc.Load(cfgFilePath);
...
using (var xmlWriter = new XmlTextWriter(cfgFilePath, Encoding.UTF8))
{
xmlWriter.Formatting = Formatting.Indented;
cfgXmlDoc.WriteTo(xmlWriter);
}
As expected, none indent the attributes. Does anyone know how to do it?
Thanks.
EDIT1
For instance, consider this piece of a XML:
<dataPortalProxies thisEndpointKind="Agent">
<dataPortalProxy isEnabled="true" name="NC Server" endpointKind="Server"
implementation="Shunra.Common.Csla.WcfOneWayProxy, Shunra.Common">
<add key="AddressTemplate" value="net.msmq://{0}/private/nc_queue"/>
</dataPortalProxy>
<dataPortalProxy isEnabled="true" name="Peer Agent" endpointKind="Agent"
implementation="Shunra.Common.Csla.WcfDynamicProxy, Shunra.Common">
<add key="AddressTemplate" value="https://{0}:7000/NCAgent/WcfPortal.svc"/>
</dataPortalProxy>
</dataPortalProxies>
Putting it through the XmlDocument yields this result:
<dataPortalProxies thisEndpointKind="Agent">
<dataPortalProxy isEnabled="true" name="NC Server" endpointKind="Server" implementation="Shunra.Common.Csla.WcfOneWayProxy, Shunra.Common">
<add key="AddressTemplate" value="net.msmq://{0}/private/nc_queue" />
</dataPortalProxy>
<dataPortalProxy isEnabled="true" name="Peer Agent" endpointKind="Agent" implementation="Shunra.Common.Csla.WcfDynamicProxy, Shunra.Common">
<add key="AddressTemplate" value="https://{0}:7000/NCAgent/WcfPortal.svc" />
</dataPortalProxy>
</dataPortalProxies>
What I want is some kind of wrap long lines for attributes, so that any attributes exceeding certain line width are indented on the following line. In short pretty printing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您永远不应该使用
new XmlTextWriter()
,它自 .NET 2.0 起已被弃用。使用
XmlWriter.Create()
代替:You should never use
new XmlTextWriter()
is has been deprecated since .NET 2.0.Use
XmlWriter.Create()
instead: