使用 XmlWriter 编写格式化的 XML

发布于 2024-12-06 06:22:01 字数 495 浏览 2 评论 0原文

我正在尝试将 XML 文件写入独立存储,但我想将其格式化为:-

<SampleData>
  <Item Property1="AliquaXX" />
  <Item Property1="Integer" />
  <Item Property1="Quisque" />
  <Item Property1="Aenean" />
  <Item Property1="Mauris" />
  <Item Property1="Vivamus" />
  <Item Property1="Nullam" />
  <Item Property1="Nam" />
  <Item Property1="Sed" />
  <Item Property1="Class" />
</SampleData>

但如果我能解决这个问题,我很困惑,有人可以帮忙吗?

I'm trying to write to an XML file to the isolated storage but I would like to format it like this:-

<SampleData>
  <Item Property1="AliquaXX" />
  <Item Property1="Integer" />
  <Item Property1="Quisque" />
  <Item Property1="Aenean" />
  <Item Property1="Mauris" />
  <Item Property1="Vivamus" />
  <Item Property1="Nullam" />
  <Item Property1="Nam" />
  <Item Property1="Sed" />
  <Item Property1="Class" />
</SampleData>

but I'm buggered if I can work it out, can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

不羁少年 2024-12-13 06:22:01

我怀疑您需要创建一个 XmlWriterSettings 具有您想要的行为(缩进等),然后在创建时将其传递给 XmlWriter。只需设置 缩进 为 true 可能就足够了:

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

I suspect you need to create an XmlWriterSettings with the behaviour you want (indentation etc) and then pass that to the XmlWriter on creation. Just setting Indent to true may well be enough:

XmlWriterSettings settings = new XmlWriterSettings { Indent = true };
using (XmlWriter writer = XmlWriter.Create(..., settings))
{
    ...
}
甜柠檬 2024-12-13 06:22:01

您可以通过 XmlWriterSettings 自定义 xml 输出。

您没有包含任何代码,但您可以在创建 XmlWriter 时设置 XmlWriterSettings。您也可以使用类似的东西:

var myXmlWriter = new XmlWriterSettings { Indent = true };

You can customize the xml output via the XmlWriterSettings.

You didn't include any code, but you can set the XmlWriterSettings when you create the XmlWriter. You can also just use something like:

var myXmlWriter = new XmlWriterSettings { Indent = true };
鸵鸟症 2024-12-13 06:22:01

如果像我一样,您正在实现自己的 XmlWriter ,您可以执行以下操作:

var myXmlWriter = new MyXmlWriter(stream, System.Text.Encoding.UTF8)
{
    Formatting = Formatting.Indented
};

或在其构造函数中执行 this.Formatting = Formatting.Indented

If, like me, you're implementing your own XmlWriter you can do:

var myXmlWriter = new MyXmlWriter(stream, System.Text.Encoding.UTF8)
{
    Formatting = Formatting.Indented
};

or do this.Formatting = Formatting.Indented in it's constructor.

知你几分 2024-12-13 06:22:01

您可以使用 DataSet.GetXML()

Dim column As DataColumn
For Each column In DataSet.Tables.Item(0).Columns
    column.ColumnMapping = MappingType.Attribute
Next
Dim xml As String = DataSet.GetXml()

它与 XmlWriter 无关,但您可以使用它来格式化 XML。

You can use DataSet.GetXML()

Dim column As DataColumn
For Each column In DataSet.Tables.Item(0).Columns
    column.ColumnMapping = MappingType.Attribute
Next
Dim xml As String = DataSet.GetXml()

It is not related to XmlWriter but you can use it for formatting XML.

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