簡體   English   中英

XElement會自動將xmlns =“”添加到自身

[英]XElement is automatically adding xmlns=“” to itself

我正在從表創建一個新的XDocument。 我必須從XSD文檔驗證文檔並且它一直失敗,因為它在不應該的時候將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>94321098761987654321323456109883</StateRequestRecordGUID>
  </EmployerTPASeparationResponse>
</EmployerTPASeparationResponseCollection>

注意元素EmployerTPASeparationResponse。 它有一個空的xmlns屬性。 我想要發生的是只編寫沒有屬性的EmployerTPASeparationResponse。

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

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

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

您需要在添加XElement時為其指定名稱空間,以使其與XDocument的名稱空間匹配。 你可以這樣做:

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

您必須為根元素創建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"/>

當您創建所有其他元素(EmployerTPASeparationResponse和StateRequestRecordGUID)時,您應該在name元素中包含命名空間(與創建EmployerTPASeparationResponseCollection時的方式相同)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM