Namespace declarations in every element

May 9, 2012 at 10:22 PM
Edited May 9, 2012 at 11:44 PM

I am experimenting with an XML Schema containing a number of imported schemas.  All of the schemas are located here.  To accomplish this, I used the following root schema document:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="" xmlns:TmatsG="" elementFormDefault="unqualified">
	<xs:import namespace="" schemaLocation="TmatsCommonTypes.xsd"/>
	<xs:import namespace="" schemaLocation="TmatsCGroup.xsd"/>
	<xs:import namespace="" schemaLocation="TmatsDGroup.xsd"/>
	<xs:import namespace="" schemaLocation="TmatsGGroup.xsd"/>
	<xs:import namespace="" schemaLocation="TmatsHGroup.xsd"/>
	<xs:import namespace="" schemaLocation="TmatsMGroup.xsd"/>
	<xs:import namespace="" schemaLocation="TmatsPGroup.xsd"/>
	<xs:import namespace="" schemaLocation="TmatsRGroup.xsd"/>
	<xs:import namespace="" schemaLocation="TmatsSGroup.xsd"/>
	<xs:import namespace="" schemaLocation="TmatsTGroup.xsd"/>
	<xs:import namespace="" schemaLocation="TmatsVGroup.xsd"/>
	<xs:element name="Tmats" type="TmatsG:Tmats">
			<xs:documentation>Tmats Root</xs:documentation>
I created classes using Linq to XSD. I then wrote the following test: 

public void TmatsXmlExample4()
    Tmats tmats = new Tmats
        ProgramName = "My Program",
        OriginationDate = DateTime.Now,
    tmats.PointOfContact.Add(new PointOfContactType
         Address = "12345 Anywhere Street",
         Agency = "My Agency",
         Name = "Robert Harvey",
         Telephone = "111-222-3333"
I expected output that looked something like this:

Tmats> <ProgramName>My Program</ProgramName> <OriginationDate>2012-05-09-07:00</OriginationDate> <PointOfContact> <Name>Robert Harvey</Name> <Agency>My Agency</Agency> <Address>12345 Anywhere Street</Address> <Telephone>111-222-3333</Telephone> </PointOfContact> </Tmats>
Instead, what I got was this:

Tmats> <ProgramName xmlns="">My Program</ProgramName> <OriginationDate xmlns="">2012-05-09-07:00</OriginationDate> <PointOfContact xmlns=""> <Name xmlns="">Robert Harvey</Name> <Agency xmlns="">My Agency</Agency> <Address xmlns="">12345 Anywhere Street</Address> <Telephone xmlns="">111-222-3333</Telephone> </PointOfContact> </Tmats>
Note the addition of the xmlns attribute to every element.  Also note the use of multiple namespaces in the output; the Tmats attribute is located in the TmatsG namespace, while the PointOfContact attribute is located in the TmatsCommon namespace. I assume that Linq to XML is adding these name space declarations to avoid ambiguity, but they aren't really necessary; all of the elements that are defined in these namespaces are unique across all of the namespaces.

Is there a way to prevent Linq to XML from generating these attributes?


Alternatively, is there a way to get the output to look like this:


  <TmatsG:ProgramName>My Program</TmatsG:ProgramName>
    <TmatsCommon:Name>Robert Harvey</TmatsCommon:Name>
   <TmatsCommon:Agency>My Agency</TmatsCommon:Agency>
    <TmatsCommon:Address>12345 Anywhere Street</TmatsCommon:Address>