2016-12-23 133 views
0

我試圖創建一個相當簡單的XML文檔,看起來像這樣。如何將xmlns添加到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>[email protected]</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)) 
); 

UPDATE

基於此下方提供的鏈接是我想出了工作。

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))); 
+0

看看這個答案:http://stackoverflow.com/questions/1542073/xdocument- or-xmldocument/1542101#1542101它有一個使用XNamespace添加名稱空間的示例。 –

+0

謝謝......該鏈接幫助。我在我原來的帖子中發佈了更新後的代碼。 – Caverman

回答

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) 
    ) 
); 
0

還有一個選項,以達到相同的結果是完全構建XElement忽略了命名空間,然後在單獨添加到代碼:

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