我試圖創建一個相當簡單的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)));
看看這個答案:http://stackoverflow.com/questions/1542073/xdocument- or-xmldocument/1542101#1542101它有一個使用XNamespace添加名稱空間的示例。 –
謝謝......該鏈接幫助。我在我原來的帖子中發佈了更新後的代碼。 – Caverman