我正在從表中創建新的XDocument。我必須從XSD文檔中驗證文檔,並且它保持失敗,因爲它將xmlns =「」添加到其中一個元素時,它不應該。以下是相關代碼的一部分。XElement自動將xmlns =「」添加到自身
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace xmlns = "https://uidataexchange.org/schemas";
XElement EmployerTPASeparationResponse = null;
XElement EmployerTPASeparationResponseCollection = new XElement(xmlns + "EmployerTPASeparationResponseCollection", new XAttribute(XNamespace.Xmlns + "xsi", xsi), new XAttribute(xsi + "schemaLocation", "https://uidataexchange.org/schemas SeparationResponse.xsd"));
XDocument doc = new XDocument(
new XDeclaration("1.0", null, "yes"), EmployerTPASeparationResponseCollection);
//sample XElement populate Element from database
StateRequestRecordGUID = new XElement("StateRequestRecordGUID");
StateRequestRecordGUID.SetValue(rdr["StateRequestRecordGUID"].ToString());
//sample to add Elements to EmployerTPASeparationResponse
EmployerTPASeparationResponse = new XElement("EmployerTPASeparationResponse");
if (StateRequestRecordGUID != null)
{
EmployerTPASeparationResponse.Add(StateRequestRecordGUID);
}
//the part where I add the EmployerTPASeparationResponse collection to the parent
EmployerTPASeparationResponseCollection.Add(EmployerTPASeparationResponse);
上述代碼產生以下xml文件。
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<EmployerTPASeparationResponseCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://uidataexchange.org/schemas SeparationResponse.xsd" xmlns="https://uidataexchange.org/schemas">
<EmployerTPASeparationResponse xmlns="">
<StateRequestRecordGUID>98761987654321323456109883</StateRequestRecordGUID>
</EmployerTPASeparationResponse>
</EmployerTPASeparationResponseCollection>
注意元素EmployerTPASeparationResponse。它有一個空的xmlns屬性。我想要發生的只是編寫沒有屬性的EmployerTPASeparationResponse。
的一點是,我不希望xmlns屬性在所有EmployerTPASeparationResponse內。有任何想法嗎? – Zach 2011-05-11 12:14:51
事實上,它存在但空白值意味着它不在名稱空間(或者更確切地說是空白的)。如果你刪除命名空間屬性(通過做我說的話,並按照下面的達斯汀),那麼你會發現該屬性不會在那裏。儘管這意味着這些節點(以及它們的子節點,除非被明確覆蓋)都在父節點的命名空間中。 – Reddog 2011-05-11 17:36:30