开发者

XElement is automatically adding xmlns="" to itself

I am creating a new XDocument from a table. I have to validate the document from an XSD document and it keeps failing because it add the xmlns="" to one of the Elements when it shouldn't. Here's parts of the code that are pertinent.

    XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
                XNamespace xmlns = "https://uidataexchange.org/schemas";
                XElement EmployerTPASeparationResponse = null;
                XElement EmployerTPASeparationResponse开发者_如何学PythonCollection = 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);

The above code produces the following xml file.

<?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>

Notice the element EmployerTPASeparationResponse. It has an empty xmlns attribute. What I want to happen is to just write EmployerTPASeparationResponse with no attributes at all.


You need to specify the namespace of the elements you are adding. e.g.

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

and

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


You need to specify the namespace for the XElement when you add it so that it matches that of the XDocument. You can do this as follows:

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


You have to create a XNamespace for the root element, and then in the creation of the element, put the object namespace created, like this:

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

The above code produce the following xml file:

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


When you create all of the other elements (EmployerTPASeparationResponse and StateRequestRecordGUID) you should that you include the namespace in the name element (in the same way that you did in the creation of your EmployerTPASeparationResponseCollection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜