如何指定一个XAttribute的命名空间,而另一个命名空间具有相同的值? [英] How to can I specify namespace to an XAttribute while having another namespace with the same value?

查看:146
本文介绍了如何指定一个XAttribute的命名空间,而另一个命名空间具有相同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有我想要做的是将数据表导出到Excel的XML文档。

All I want to do is an XML document for exporting my datatable to Excel.

所以我需要的是这样的:

So what I need is something like this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?mso-application Excel.Sheet?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" />    

我正在使用 System.Xml.Linq 我几乎有它,但我的代码不断添加ss到工作簿的前面。这是我的代码:

I am using System.Xml.Linq and I almost have it, but my code keeps adding "ss" to the front of Workbook. This is my code:

    XDocument xmlssDoc2 = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new 
    XProcessingInstruction("mso-application", "Excel.Sheet"));

    XNamespace aw = "urn:schemas-microsoft-com:office:spreadsheet";
    XNamespace fc = "urn:schemas-microsoft-com:office:spreadsheet";
        XElement root = new XElement(aw + "Workbook",
            new XAttribute("xmlns", "urn:schemas-microsoft-com:office:spreadsheet"),
            new XAttribute(XNamespace.Xmlns + "ss", "urn:schemas-microsoft-com:office:spreadsheet")
        );        

我得到的结果是:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?mso-application Excel.Sheet?>
<ss:Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" />    

请帮助!

推荐答案

参考源 for XElement ,它看起来好像命名空间/前缀属性对被推送到一个推下堆栈,然后检查匹配来自从上到下的元素命名空间/ a>的堆栈 - 有效地进行匹配,以相反的顺序添加属性。

From the reference source for XElement, it looks as though the namespace/prefix attribute pairs are pushed onto a push-down stack in order of addition while writing, then checked for matches against the element namespace from top to bottom of the stack -- effectively doing the match in reverse order in which the attributes are added.

所以如果以相反的顺序添加命名空间, code> ss 被省略:

So if you add the namespaces in the opposite order, the ss is omitted:

        XDocument xmlssDoc2 = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new
        XProcessingInstruction("mso-application", "Excel.Sheet"));

        XNamespace blank = "urn:schemas-microsoft-com:office:spreadsheet";
        XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet";

        XElement root = new XElementTest(blank + "Workbook");
        root.Add(new XAttribute(XNamespace.Xmlns + "ss", ss));
        root.Add(new XAttribute("xmlns", blank));

        Debug.WriteLine(root.ToString());

产生:

<Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns="urn:schemas-microsoft-com:office:spreadsheet" />  

当然,这意味着 xmlns 属性规格已更改,但理想情况下,此不应该重要根据XML规范。

Of course, this means that the order of the xmlns attribute specifications has changed, however ideally this should not matter according to the XML specification.

这篇关于如何指定一个XAttribute的命名空间,而另一个命名空间具有相同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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