简体   繁体   English

如何在 C# 中使用 XDocument 在根元素中生成具有多个命名空间的 XML 文件?

[英]How to generate a XML-file with multiple namespaces in root element with XDocument in C#?

I want to generate a custom manifest.xml file for an Outlook Add-In using XDocument in C#.我想在 C# 中使用 XDocument 为 Outlook 加载项生成自定义 manifest.xml 文件。

My current XML file (what I want to generate now) looks as following:我当前的 XML 文件(我现在要生成的文件)如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<OfficeApp xmlns="http://schemas.microsoft.com/office/appforoffice/1.1"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:bt="http://schemas.microsoft.com/office/officeappbasictypes/1.0"
           xmlns:mailappor="http://schemas.microsoft.com/office/mailappversionoverrides"
           xsi:type="MailApp">
  <Id>{0026EAB0-AFBA-43FE-A3FA-C479B6FEECCA}</Id>
  <Version>2.0.0.0</Version>
  <!-- More elements -->
</OfficeApp>

My main problem is, that I can't manage to add multiple namespaces to the OfficeApp-element.我的主要问题是,我无法向 OfficeApp 元素添加多个命名空间。

I already tried the following:我已经尝试了以下方法:

private readonly XNamespace Xsi = "xsi:";
private readonly XNamespace MicrosoftSchemasAppsForOffice = "http://schemas.microsoft.com/office/appforoffice/1.1";
private readonly XNamespace W3 = "http://www.w3.org/2001/XMLSchema-instance";
private readonly XNamespace MicrosoftSchemasOfficeBasicTypes = "http://schemas.microsoft.com/office/officeappbasictypes/1.0";
private readonly XNamespace MicrosoftSchemasMailAppVersion = "http://schemas.microsoft.com/office/mailappversionoverrides";

private XDocument GenerateDocument()
{
    return new XDocument(
      new XDeclaration("1.0", "utf-8", null),
      new XElement("OfficeApp",
        new XAttribute("xmlns", MicrosoftSchemasAppsForOffice),
        new XAttribute(XNamespace.Xmlns + "xsi", W3),
        new XAttribute(XNamespace.Xmlns + "bt", MicrosoftSchemasOfficeBasicTypes),
        new XAttribute(XNamespace.Xmlns + "mailappor", MicrosoftSchemasMailAppVersion),
        new XAttribute(Xsi + "type", "MailApp"),
        new XElement("Id", "{" + Guid.NewGuid().ToString() + "}"),
        new XElement("Version", "2.0.0.0")
      )
    );
}

Which leaded to the following Exception:这导致了以下异常:

System.Xml.XmlException: 'The prefix '' cannot be redefined from '' to    'http://schemas.microsoft.com/office/appforoffice/1.1' within the same start element tag.'

I also tried to replace line我也尝试更换线

new XAttribute("xmlns", MicrosoftSchemasAppsForOffice),

with

new XAttribute(XNamespace.Xmlns.NamespaceName, MicrosoftSchemasAppsForOffice),

but this gave me the following Exception:但这给了我以下异常:

System.Xml.XmlException: 'The ':' character, hexadecimal value 0x3A, cannot be included in a name.'

After hours of trying and failing I still don't know how to handle the xmlns-namespace properly.经过数小时的尝试和失败,我仍然不知道如何正确处理 xmlns-namespace。 What am I doing wrong?我究竟做错了什么? I think the XML-Code I am trying to generate is valid as it works pretty fine.我认为我试图生成的 XML 代码是有效的,因为它工作得很好。

I'm thankful for any hint.我很感激任何提示。 Thanks!谢谢!

Your root namespace doesn't have to be added as attribute, just used as namespace for elements.您的根命名空间不必作为属性添加,只需用作元素的命名空间。 Also, you have to use xsi namespace value with the type attribute:此外,您必须将 xsi 命名空间与 type 属性一起使用:

private readonly XNamespace MicrosoftSchemasAppsForOffice = "http://schemas.microsoft.com/office/appforoffice/1.1";
private readonly XNamespace W3 = "http://www.w3.org/2001/XMLSchema-instance";
private readonly XNamespace MicrosoftSchemasOfficeBasicTypes = "http://schemas.microsoft.com/office/officeappbasictypes/1.0";
private readonly XNamespace MicrosoftSchemasMailAppVersion = "http://schemas.microsoft.com/office/mailappversionoverrides";

private XDocument GenerateDocument()
{
    return new XDocument(
      new XDeclaration("1.0", "utf-8", null),
      new XElement(MicrosoftSchemasAppsForOffice + "OfficeApp",
        new XAttribute(XNamespace.Xmlns + "xsi", W3),
        new XAttribute(XNamespace.Xmlns + "bt", MicrosoftSchemasOfficeBasicTypes),
        new XAttribute(XNamespace.Xmlns + "mailappor", MicrosoftSchemasMailAppVersion),
        new XAttribute(W3 + "type", "MailApp"),
        new XElement(MicrosoftSchemasAppsForOffice + "Id", "{" + Guid.NewGuid().ToString() + "}"),
        new XElement(MicrosoftSchemasAppsForOffice + "Version", "2.0.0.0")
      )
    );
}

Try getting namespace right is hard in XDocument so I normally just parse a string尝试在 XDocument 中正确获取命名空间是很困难的,所以我通常只解析一个字符串

            string xml = 
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<OfficeApp xmlns=\"http://schemas.microsoft.com/office/appforoffice/1.1\"" +
                   " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
                   " xmlns:bt=\"http://schemas.microsoft.com/office/officeappbasictypes/1.0\"" +
                   " xmlns:mailappor=\"http://schemas.microsoft.com/office/mailappversionoverrides\"" +
                   " xsi:type=\"MailApp\">" +
                "</OfficeApp>";

            XDocument doc = XDocument.Parse(xml);
            XElement officeApp = doc.Root;
            XNamespace ns = officeApp.GetDefaultNamespace();
            officeApp.Add(new XElement(ns + "Id","{0026EAB0-AFBA-43FE-A3FA-C479B6FEECCA}"));
            officeApp.Add(new XElement(ns + "Version","2.0.0.0"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM