2011-05-10 52 views
18

我正在從表中創建新的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。

回答

9

您需要指定要添加的元素的命名空間。例如

//sample XElement populate Element from database 
StateRequestRecordGUID = new XElement(xmlns + "StateRequestRecordGUID"); 

//sample to add Elements to EmployerTPASeparationResponse 
EmployerTPASeparationResponse = new XElement(xmlns + "EmployerTPASeparationResponse"); 
0

當您創建的所有其他元素(EmployerTPASeparationResponse和StateRequestRecordGUID),你應該,你在名稱中包含元素的命名空間(在你創建一個你EmployerTPASeparationResponseCollection的也做了同樣的方式。

+0

的一點是,我不希望xmlns屬性在所有EmployerTPASeparationResponse內。有任何想法嗎? – Zach 2011-05-11 12:14:51

+0

事實上,它存在但空白值意味着它不在名稱空間(或者更確切地說是空白的)。如果你刪除命名空間屬性(通過做我說的話,並按照下面的達斯汀),那麼你會發現該屬性不會在那裏。儘管這意味着這些節點(以及它們的子節點,除非被明確覆蓋)都在父節點的命名空間中。 – Reddog 2011-05-11 17:36:30

9

您需要在添加XElement時指定名稱空間,以便它與XDocument的名稱空間相匹配。你可以做到這一點,如下所示:

XElement employerTPASeperationResponse = 
    new XElement(xmlns + "EmployerTPASeparationResponse"); 
+2

問題是,我不想在Employer的TPASeparationResponse中使用xmlns屬性。有任何想法嗎? – Zach 2011-05-11 12:17:55

+2

@Zach:添加上面應該刪除它,因爲它位於已經指定它的元素下面。該體系結構爲您處理該部分。 – 2011-05-11 12:21:27

+2

工作正常!現在我必須在每個添加元素的位置都包含xmlns +「ElementName」。謝謝!! – Zach 2011-05-11 12:27:10

3

你必須創建根元素的的XNamespace,然後在創作的元素,把對象的命名空間創建的,是這樣的:

xmlDoc = new XDocument(); 
xmlDoc.Declaration = new XDeclaration("1.0", "utf-8", null); 

XNamespace pageDefinition = @"http://xmlns.oracle.com/adfm/uimodel"; 

XElement root = new XElement(pageDefinition + "pageDefinition", new XAttribute("Package", "oracle.webcenter.portalapp.pages")); 

xmlDoc.Add(root); 

上面的代碼產生下面的XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<pageDefinition xmlns="http://xmlns.oracle.com/adfm/uimodel" Package="oracle.webcenter.portalapp.pages"/>