C# 如何使用命名空间和 XElement 前缀编写 xml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1338517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:18:25  来源:igfitidea点击:

How can I write xml with a namespace and prefix with XElement?

c#xmllinq-to-xml

提问by Chris Conway

This may be a beginner xml question, but how can I generate an xml document that looks like the following?

这可能是一个初学者的 xml 问题,但如何生成如下所示的 xml 文档?

<root xmlns:ci="http://somewhere.com" xmlns:ca="http://somewhereelse.com">
    <ci:field1>test</ci:field1>
    <ca:field2>another test</ca:field2>
</root>

If I can get this to be written, I can get the rest of my problem to work.

如果我能把这个写出来,我就能解决剩下的问题。

Ideally, I'd like to use LINQ to XML (XElement, XNamespace, etc.) with c#, but if this can be accomplished easier/better with XmlDocuments and XmlElements, I'd go with that.

理想情况下,我想在 c# 中使用 LINQ to XML(XElement、XNamespace 等),但如果使用 XmlDocuments 和 XmlElements 可以更轻松/更好地实现这一点,我会选择这样做。

Thanks!!!

谢谢!!!

采纳答案by Andrew Hare

Here is a small example that creates the output you want:

这是一个创建所需输出的小示例:

using System;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        XNamespace ci = "http://somewhere.com";
        XNamespace ca = "http://somewhereelse.com";

        XElement element = new XElement("root",
            new XAttribute(XNamespace.Xmlns + "ci", ci),
            new XAttribute(XNamespace.Xmlns + "ca", ca),
                new XElement(ci + "field1", "test"),
                new XElement(ca + "field2", "another test"));
    }
}

回答by blparker

XNamespace ci = "http://somewhere.com";
XNamespace ca = "http://somewhereelse.com";
XElement root = new XElement(aw + "root",
    new XAttribute(XNamespace.Xmlns + "ci", "http://somewhere.com"),
    new XAttribute(XNamespace.Xmlns + "ca", "http://somewhereelse.com"),
    new XElement(ci + "field1", "test"),
    new XElement(ca + "field2", "another test")
);
Console.WriteLine(root);

This should output

这应该输出

<root xmlns:ci="http://somewhere.com" xmlns:ca="http://somewhereelse.com">
    <ci:field1>test</ci:field1>
    <ca:field2>another test</ca:field2>
</root>

回答by Brandon Hawbaker

For XmlDocument it's similar:

对于 XmlDocument,它是类似的:

XmlAttribute attribute1 = sessionXml.CreateAttribute("s", "Attribute1", namespaceURI);
XmlAttribute attribute2 = sessionXml.CreateAttribute("s", "Attribute2", namespaceURI);
XmlAttribute attribute3 = sessionXml.CreateAttribute("s", "Attribute3", namespaceURI);
XmlAttribute attribute4 = sessionXml.CreateAttribute("s", "Attribute4", namespaceURI);

回答by Hariharan

Try this code:

试试这个代码:

string prefix = element.GetPrefixOfNamespace(element.Name.NamespaceName);
string name = String.Format(prefix == null ? "{1}" : "{0}:{1}", prefix, element.Name.LocalName);`