22

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

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.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Zach
  • 329
  • 1
  • 3
  • 8

4 Answers4

12

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");
Dustin Hodges
  • 4,110
  • 3
  • 26
  • 41
10

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");
Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
  • 3
    The point is that I don't want the xmlns attribute at all within the EmployerTPASeparationResponse. Any ideas? – Zach May 11 '11 at 12:17
  • 3
    @Zach: Adding the above should remove it as it is sat under an element that already specifies it. The architecture handles that part for you. – Jeff Yates May 11 '11 at 12:21
  • 3
    That worked! Now I have to include xmlns + "ElementName" every place I add an element. Thank you!! – Zach May 11 '11 at 12:27
3

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"/>
jjimenez
  • 31
  • 1
0

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.

Reddog
  • 15,219
  • 3
  • 51
  • 63
  • The point is that I don't want the xmlns attribute at all within the EmployerTPASeparationResponse. Any ideas? – Zach May 11 '11 at 12:14
  • The fact that it's there but with the blank value means that it's not in a namespace at all (or rather the blank one). If you remove the namespace attribute (by doing what I said, and as per Dustin below) then you'll find that the attribute won't be there. Though the implication is that those nodes (and their children unless explicitly overwritten) are in parent's namespace. – Reddog May 11 '11 at 17:36