我怎么能写与一个的XElement命名空间和preFIX XML? [英] How can I write xml with a namespace and prefix with XElement?

查看:129
本文介绍了我怎么能写与一个的XElement命名空间和preFIX XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个初学者XML的问题,但我怎么能生成一个XML文档,看起来像下面这样?

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

<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.

在理想情况下,我想用C#使用LINQ到XML(的XElement,的XNamespace等),但如果这可以完成更容易/与XmlDocuments和XmlElements更好,我会去这一点。

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.

谢谢!

推荐答案

下面是创建您需要的输出一个小例子:

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"));
    }
}

这篇关于我怎么能写与一个的XElement命名空间和preFIX XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆