【发布时间】:2020-08-28 12:05:37
【问题描述】:
我想在 C# 中使用 XDocument 为 Outlook 加载项生成自定义 manifest.xml 文件。
我当前的 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>
我的主要问题是,我无法将多个命名空间添加到 OfficeApp 元素。
我已经尝试了以下方法:
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")
)
);
}
导致以下异常:
System.Xml.XmlException: 'The prefix '' cannot be redefined from '' to 'http://schemas.microsoft.com/office/appforoffice/1.1' within the same start element tag.'
我也试过换行
new XAttribute("xmlns", MicrosoftSchemasAppsForOffice),
与
new XAttribute(XNamespace.Xmlns.NamespaceName, MicrosoftSchemasAppsForOffice),
但这给了我以下异常:
System.Xml.XmlException: 'The ':' character, hexadecimal value 0x3A, cannot be included in a name.'
经过数小时的尝试和失败,我仍然不知道如何正确处理 xmlns 命名空间。 我究竟做错了什么?我认为我尝试生成的 XML 代码是有效的,因为它工作得很好。
感谢任何提示。谢谢!
【问题讨论】:
标签: c# xml linq-to-xml xml-namespaces