C# XML 编写器 - 使用哪些类型/对象?

发布于 2025-01-11 23:54:28 字数 809 浏览 0 评论 0原文

简介: 各位编码员大家好;我创建了一个项目来创建 XML 文件,以便使用某些设置自动创建文件。

问题:我的知识不足以重现这一点。我找不到关于两个问题的正确信息;

A. 如何在元素内部创建元素(使用第二个 writestartelement 关闭第一个元素);

B. 第 3 行有一个我不认识的元素。谁能告诉我应该在网上寻找哪种类型?

<userEnvironmentSettings>
    <conditions>
        <ref n="Printer - PR123456.xml" />
    </conditions>
</userEnvironmentSettings>

当前代码(将保持最新)

                writer.WriteStartElement("userEnvironmentSettings");
                writer.WriteStartElement("conditions");
                writer.WriteAttributeString("ref n", "Printer - " + printerName + ".xml\"");
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.Flush();

结果:无法在 Attributestring 内设置空格的 $exception。

Intro:
Hi fellow coders; I created a project to create XML files to automate filecreation with certain settings.

Issue: my knowledge isn't sufficient to recreate this. I can't find the right information on two questions;

A. How to create an element inside an element (using a second writestartelement closes the first one);

B. On line 3 there's an element which I do not recognise. Can anybody tell me which type I should be looking for on the net?

<userEnvironmentSettings>
    <conditions>
        <ref n="Printer - PR123456.xml" />
    </conditions>
</userEnvironmentSettings>

Current code (Will keep this up to date)

                writer.WriteStartElement("userEnvironmentSettings");
                writer.WriteStartElement("conditions");
                writer.WriteAttributeString("ref n", "Printer - " + printerName + ".xml\"");
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.Flush();

Result: $exception as per a whitespace cannot be set inside Attributestring.

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

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

发布评论

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

评论(1

好倦 2025-01-18 23:54:28

您需要一个名为 ref元素,以及一个名为 n属性。您尝试做的是在 conditions 元素中创建一个名为 ref n 的属性,这是不允许的(因为它包含空格)并且不是您想要的。所以这可行:

writer.WriteStartElement("userEnvironmentSettings");
writer.WriteStartElement("conditions");
writer.WriteStartElement("ref");
writer.WriteAttributeString("n", "Printer - PR123456.xml");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();

但是,除非您有充分的理由,否则我强烈建议您不要直接使用XmlWriter。您可以使用更简单、更高级别的 API。例如:

var doc = new XDocument(
    new XElement("userEnvironmentSettings",
        new XElement("conditions",
            new XElement("ref",
                new XAttribute("n", "Printer - PR123456.xml")
            ))));
doc.Save(fileName);

You need an element called ref with an attribute called n. What you've tried to do is create an attribute called ref n within the conditions element, which is not allowed (as it contains whitespace) and is not what you want. So this would work:

writer.WriteStartElement("userEnvironmentSettings");
writer.WriteStartElement("conditions");
writer.WriteStartElement("ref");
writer.WriteAttributeString("n", "Printer - PR123456.xml");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();

However, unless you have good reason I'd strongly suggest you don't use XmlWriter directly. There are much easier, higher-level APIs you can use. For example:

var doc = new XDocument(
    new XElement("userEnvironmentSettings",
        new XElement("conditions",
            new XElement("ref",
                new XAttribute("n", "Printer - PR123456.xml")
            ))));
doc.Save(fileName);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文