在XDocument中使用':'字符生成Excel - C#

jas*_*met 6 excel linq-to-xml

我对编程世界很陌生,所以这个问题可能很愚蠢,但我被困住了,希望得到一些帮助.

我正在使用XDocument将信息更改并添加到Excel工作表(这是一个XML文档),在项目中从Autodesk Revit中获取Excel报表.工作表文档由工作表信息组成,包括此配置中的行和单元格:

<row r="11" spans="1:11" x14ac:dyDescent="0.2">
    <c r="A11" s="198" t="inlineStr">
        <is>
            <t>example</t>
        </is>
    </c>
    <c r="B11" s="199" t="inlineStr">
        <is>
            <t>string</t>
        </is>
    </c>
    <c r="C11" s="200"/>
    <c r="D11" s="201"/>
    <c r="E11" s="201"/>
    <c r="F11" s="202"/>
    <c r="G11" s="203"/>
    <c r="H11" s="204"/>
    <c r="I11" s="205"/>
    <c r="J11" s="205"/>
    <c r="K11" s="206"/>
</row>
Run Code Online (Sandbox Code Playgroud)

代码的相关部分位于row-element中.spans-attribute的值为"1:11",这就是我的问题所在.它不会让我输入':'字符作为属性的值.我搜索过网络,发现它与此链接上的名称空间声明有关: "''''字符,十六进制值0x3A,不能包含在名称中"

但是,将此":"字符放入属性值对于Excel文档的工作是必不可少的.我按如下方式创建row元素:

XElement row = new XElement("row",
                    new XAttribute("r", i.ToString()),
                    new XAttribute("spans", "1:" + collumnCount.ToString()),
                    new XAttribute("x14ac:dyDescent", "0.2"));
Run Code Online (Sandbox Code Playgroud)

我不明白为什么它不允许我在XAttribute的值中加上':',因为这只是一个字符串.有没有办法让这项工作?

我尝试使用XMLDocument将字符串"1:11"添加到XMLAttribute.这确实有效,但我不相信XDocument是不可能的.

提前致谢

Jus*_*ner 0

您应该将其包含:在元素名称中。现在已经允许了。如果您看到有人这样做,那么这:不是元素名称的一部分,而是命名空间和元素名称之间的分隔符。所以为了得到下面的XML文件:

<rss xmlns:media="http://m.xyz.com/rss/" version="2.0">
    <channel vendor="ABC" lastpublishdate="05/12/2013 01:02"/>
    <title>Videos</title>
    <media:thumbnail width="180" height="75" />
</rss>
Run Code Online (Sandbox Code Playgroud)

你必须写一些类似的东西:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string x = "55";

            XNamespace aw = "http://m.xyz.com/rss/";
            XDocument document = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"), 
                new XElement(
                    "rss", 
                    new XAttribute(XNamespace.Xmlns + "media", "http://m.xyz.com/rss/"), 
                    new XAttribute("version", "2.0"),

                    new XElement("channel",
                        new XAttribute("vendor", "ABC"),
                        new XAttribute("lastpublishdate", DateTime.Now.ToString("dd/MM/yyyy hh:mm"))),

                        new XElement("title", "Videos"),
                        new XElement(
                            aw + "thumbnail", 
                            new XAttribute("width", x.Trim()), 
                            new XAttribute("height", x.Trim())
                        )
                )
            );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)