如何输出 XmlDocument 以便元素属性也缩进?

发布于 2024-10-07 17:26:15 字数 1993 浏览 4 评论 0原文

我想输出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷途知返 2024-10-14 17:26:15

您永远不应该使用 new XmlTextWriter(),它自 .NET 2.0 起已被弃用。

使用 XmlWriter.Create() 代替:

XmlWriterSettings settings = 
    new XmlWriterSettings {Indent = true, NewLineOnAttributes = true};
using (var writer = XmlWriter.Create("path", settings))
{

}

You should never use new XmlTextWriter() is has been deprecated since .NET 2.0.

Use XmlWriter.Create() instead:

XmlWriterSettings settings = 
    new XmlWriterSettings {Indent = true, NewLineOnAttributes = true};
using (var writer = XmlWriter.Create("path", settings))
{

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文