C# XML 编写器 - 使用哪些类型/对象?
简介: 各位编码员大家好;我创建了一个项目来创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要一个名为
ref
的元素,以及一个名为n
的属性。您尝试做的是在conditions
元素中创建一个名为ref n
的属性,这是不允许的(因为它包含空格)并且不是您想要的。所以这可行:但是,除非您有充分的理由,否则我强烈建议您不要直接使用
XmlWriter
。您可以使用更简单、更高级别的 API。例如:You need an element called
ref
with an attribute calledn
. What you've tried to do is create an attribute calledref n
within theconditions
element, which is not allowed (as it contains whitespace) and is not what you want. So this would work: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: