如何将xmlns添加到xml文档?

时间:2016-12-23 18:00:14

标签: c# xml linq-to-xml xml-namespaces

我正在尝试创建一个看起来像这样的相当简单的XML文档。

<?xml version="1.0" encoding="UTF-8"?>
<employees xmlns="http://website.com/xsd/MQ">
    <employee>
        <id>00122731</id>
        <first-name>LUIS</first-name>
        <last-name>GARCIA</last-name>
        <subarea>4100</subarea>
        <cost-center>904</cost-center>
        <email-address>L.GARCIA@EMAIL.COM</email-address>
    </employee>
</employees>

我能够通过使用此代码获得基础知识,但我需要添加xmlns属性,我不知道如何做到这一点。

var xmlDoc = new XElement("employees",
from e in listEmployees
select new XElement("employee",
    new XElement("id", e.EmployeeId),
    new XElement("first-name", e.FirstName),
    new XElement("last-name", e.LastName),
    new XElement("subarea", e.SubArea),
    new XElement("cost-center", e.CostCenter),
    new XElement("email-address", e.EmailAddress)));

这是我尝试添加它但是我得到一个错误,这会导致结构无效。

XDocument xmlDoc = new XDocument(
    new XElement("employees",
        new XAttribute("xmlns", "http://website/xsd/MQ")),                        
        from e in listEmployees
        select new XElement("employee",
            new XElement("id", e.EmployeeId),
            new XElement("first-name", e.FirstName),
            new XElement("last-name", e.LastName),
            new XElement("subarea", e.SubArea),
            new XElement("cost-center", e.CostCenter),
            new XElement("email-address", e.EmailAddress))
);

更新

根据下面提供的链接,这是我提出的有效方法。

XNamespace ns = "http://website/xsd/MQ";
var xmlDoc = new XElement(ns + "employees",
from e in listEmployees
select new XElement("employee",
    new XElement("id", e.EmployeeId),
    new XElement("first-name", e.FirstName),
    new XElement("last-name", e.LastName),
    new XElement("subarea", e.SubArea),
    new XElement("cost-center", e.CostCenter),
    new XElement("email-address", e.EmailAddress)));

2 个答案:

答案 0 :(得分:2)

您需要在所有元素上包含命名空间,而不仅仅是最重要的元素:

XNamespace ns = "http://website/xsd/MQ";
var xmlDoc = new XElement(ns + "employees",
    from e in listEmployees
    select new XElement(ns + "employee",
        new XElement(ns + "id", e.EmployeeId),
        new XElement(ns + "first-name", e.FirstName),
        new XElement(ns + "last-name", e.LastName),
        new XElement(ns + "subarea", e.SubArea),
        new XElement(ns + "cost-center", e.CostCenter),
        new XElement(ns + "email-address", e.EmailAddress)
    )
);

如果这对你来说太重复了,你可以做一个方便的方法:

XNamespace ns = "http://website/xsd/MQ";
private static XElement MQElement(string name, object contents)
{
    return new XElement(ns + name, contents);
}

然后使用它:

var xmlDoc = MQElement("employees",
    from e in listEmployees
    select MQElement("employee",
        MQElement("id", e.EmployeeId),
        MQElement("first-name", e.FirstName),
        MQElement("last-name", e.LastName),
        MQElement("subarea", e.SubArea),
        MQElement("cost-center", e.CostCenter),
        MQElement("email-address", e.EmailAddress)
    )
);

答案 1 :(得分:0)

实现相同结果的另一个选择是构造XElement完全忽略名称空间,然后将它们添加到单独的代码中:

foreach (XElement e in xmlDoc.DescendantsAndSelf())
{
  if (e.Name.Namespace == "")
  {
    e.Name = ns + e.Name.LocalName;
  }
}