By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,523 Members | 1,938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,523 IT Pros & Developers. It's quick & easy.

XSD to Class to XML file results in elements with xmlns="" tag

P: 3
Hi all,

Hope someone is able and willing to help me with following problem.
I received a xsd file from another company, our company i supposed to return xml based on that xsd.
Problem is that I don't really understand how these namespace work in xml.
(I am however aware of what problems namespaces solve)
I'm not even sure if the provided xsd is 'common' practice, although it validates correctly.

So I'll describe exactly what I've been doing:

I've converted the .xsd file (i posted contents below) to VB classes with the VS.NET 2005 xsd command:

Expand|Select|Wrap|Line Numbers
  1. xsd source.xsd /c /l:vb
Contents of the source.xsd file:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-16"?>
  2. <xsd:schema xmlns="http://www.company.net/product" xmlns:tellit="http://www.company.net/product" targetNamespace="http://www.company.net/product" version="7.6" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified">
  3.     <xsd:element name="root">
  4.         <xsd:complexType>
  5.             <xsd:sequence>
  6.                 <xsd:element minOccurs="0" maxOccurs="unbounded" name="partner-dto">
  7.                     <xsd:annotation>
  8.                         <xsd:documentation>This is the information about partners (customers - suppliers).</xsd:documentation>
  9.                     </xsd:annotation>
  10.                     <xsd:complexType>
  11.                         <xsd:all>
  12.                             <xsd:element minOccurs="0" maxOccurs="1" name="internal-reference" type="xsd:string">
  13.                                 <xsd:annotation>
  14.                                     <xsd:documentation>DB Primary key in Deal-IT.</xsd:documentation>
  15.                                 </xsd:annotation>
  16.                             </xsd:element>
  17.                             <xsd:element minOccurs="1" maxOccurs="1" name="external-reference" type="xsd:string">
  18.                                 <xsd:annotation>
  19.                                     <xsd:documentation>DB Primary key in external ERP system.</xsd:documentation>
  20.                                 </xsd:annotation>
  21.                             </xsd:element>
  22.                             <xsd:element minOccurs="1" maxOccurs="1" name="functional-reference" type="xsd:string">
  23.                                 <xsd:annotation>
  24.                                     <xsd:documentation>Functional key.</xsd:documentation>
  25.                                 </xsd:annotation>
  26.                             </xsd:element>
  27.                             <xsd:element minOccurs="1" maxOccurs="1" name="description" type="xsd:string">
  28.                                 <xsd:annotation>
  29.                                     <xsd:documentation>Extra information.</xsd:documentation>
  30.                                 </xsd:annotation>
  31.                             </xsd:element>
  32.                             <xsd:element minOccurs="1" maxOccurs="1" name="allow-partial-deliveries" type="xsd:boolean">
  33.                                 <xsd:annotation>
  34.                                     <xsd:documentation>Flag that indicates that partial deliveries for this partner are allowed.</xsd:documentation>
  35.                                 </xsd:annotation>
  36.                             </xsd:element>
  37.                             <xsd:element minOccurs="0" maxOccurs="1" name="main-address-reference" type="xsd:string">
  38.                                 <xsd:annotation>
  39.                                     <xsd:documentation>The id of the main address. The addresses are defined via the address-dto</xsd:documentation>
  40.                                 </xsd:annotation>
  41.                             </xsd:element>
  42.                             <xsd:element minOccurs="1" maxOccurs="1" name="state-reference" type="xsd:string">
  43.                                 <xsd:annotation>
  44.                                     <xsd:documentation>external-reference to the partner status.</xsd:documentation>
  45.                                 </xsd:annotation>
  46.                             </xsd:element>
  47.                         </xsd:all>
  48.                         <xsd:attribute name="message-status" use="required">
  49.                             <xsd:annotation>
  50.                                 <xsd:documentation>Indicates the transaction type: I=Insert U=Update D=Delete.</xsd:documentation>
  51.                             </xsd:annotation>
  52.                             <xsd:simpleType>
  53.                                 <xsd:restriction base="xsd:token">
  54.                                     <xsd:length value="1" />
  55.                                     <xsd:enumeration value="I" />
  56.                                     <xsd:enumeration value="U" />
  57.                                     <xsd:enumeration value="D" />
  58.                                 </xsd:restriction>
  59.                             </xsd:simpleType>
  60.                         </xsd:attribute>
  61.                     </xsd:complexType>
  62.                 </xsd:element>
  63.             </xsd:sequence>
  64.         </xsd:complexType>
  65.     </xsd:element>
  66. </xsd:schema>
Next I fill the generated classes through code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim _root As New root
  3.  
  4.         Dim _partner As New rootPartnerdto
  5.         With _partner
  6.             .internalreference = ""
  7.             .externalreference = "1564"
  8.             .functionalreference = "1564 Naamklant"
  9.             .description = "Naamklant"
  10.             .allowpartialdeliveries = True
  11.             .mainaddressreference = ""
  12.             .statereference = ""
  13.             .messagestatus = rootPartnerdtoMessagestatus.I
  14.         End With
  15.  
  16.  
  17.         Dim _partner2 As New rootPartnerdto
  18.         With _partner2
  19.             .internalreference = ""
  20.             .externalreference = "1564"
  21.             .functionalreference = "1564 Naamklant"
  22.             .description = "Naamklant"
  23.             .allowpartialdeliveries = True
  24.             .mainaddressreference = ""
  25.             .statereference = ""
  26.             .messagestatus = rootPartnerdtoMessagestatus.I
  27.         End With
  28.         'lst.Add(_partner2)
  29.  
  30.         _root.partnerdto = New rootPartnerdto() {_partner, _partner2}
  31.  
  32.         Dim _output As String = PCNObjecten.XMLExport.Export(_root)
  33.  
The export function works like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Public Shared Function Export(ByVal _root As root) As String
  4.  
  5.         Dim _output As String
  6.  
  7.         Dim writer As New XmlSerializer(GetType(root)) 
  8.         Dim _str As New MemoryStream
  9.         writer.Serialize(_str, _root)
  10.         _str.Flush()
  11.         _str.Seek(0, SeekOrigin.Begin)
  12.         Dim _rdr As New StreamReader(_str)
  13.         _output = _rdr.ReadToEnd()
  14.  
  15.         Debug.WriteLine(_output)
  16.         Return _output
  17.  
  18.     End Function
  19.  
The output xml:

Expand|Select|Wrap|Line Numbers
  1. <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.company.net/product">
  2.   <partner-dto message-status="I" xmlns="">
  3.     <internal-reference />
  4.     <external-reference>1564</external-reference>
  5.     <functional-reference>1564 Naamklant</functional-reference>
  6.     <description>Naamklant</description>
  7.     <allow-partial-deliveries>true</allow-partial-deliveries>
  8.     <main-address-reference />
  9.     <state-reference />
  10.   </partner-dto>
  11.   <partner-dto message-status="I" xmlns="">
  12.     <internal-reference />
  13.     <external-reference>1564</external-reference>
  14.     <functional-reference>1564 Naamklant</functional-reference>
  15.     <description>Naamklant</description>
  16.     <allow-partial-deliveries>true</allow-partial-deliveries>
  17.     <main-address-reference />
  18.     <state-reference />
  19.   </partner-dto>
  20. </root>
  21.  
Now the question part:
- Why is that xmlns attribute empty? I think it should not be present, since the default namespace should be "http://www.company.net/product"?
- What am I doing wrong, or is there something wrong with the .xsd definition?
Can I fix this without changing the .xsd definition?

Thanks if you took the time to read or even run this code.
Regards,

Boris
Aug 29 '07 #1
Share this Question
Share on Google+
4 Replies


jkmyoung
Expert 100+
P: 2,057
In your schema, you've got elementFormDefault="unqualified" which will put the elements you declare in there in the default xmlns="" namespace.

Seems the problem is telling the program that you want those elements to be in the same namespace when you generate the schema with the xsd command.
Aug 29 '07 #2

P: 3
Thanks for your answer,

So as long if the xsd sais elementFormDefault=Unqualified, the output will always contain the empty xmlns="" tag?

Is that xmlns="" tag valid? Or will our xml cause validationerrors?

I ran some tests with different elementFormDefault and attributeFormDefault settings, here are the results:

with attributeFormDefault = UnQualified
and elementFormDefault = UnQualified:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.company.net/product">
  3.   <partner-dto message-status="I" xmlns="">
  4.     <internal-reference />
  5.     <external-reference>1564</external-reference>
  6.     <functional-reference>1564 Naamklant</functional-reference>
  7.     <description>Naamklant</description>
  8.     <allow-partial-deliveries>true</allow-partial-deliveries>
  9.     <main-address-reference />
  10.     <state-reference />
  11.   </partner-dto>
  12.   <partner-dto message-status="I" xmlns="">
  13.     <internal-reference />
  14.     <external-reference>1564</external-reference>
  15.     <functional-reference>1564 Naamklant</functional-reference>
  16.     <description>Naamklant</description>
  17.     <allow-partial-deliveries>true</allow-partial-deliveries>
  18.     <main-address-reference />
  19.     <state-reference />
  20.   </partner-dto>
  21.   <sales-order-dto message-status="I" xmlns="">
  22.     <delivery-address-reference xsi:nil="true" />
  23.     <ship-together>true</ship-together>
  24.     <sales-reference />
  25.     <order-date>0001-01-01T00:00:00</order-date>
  26.   </sales-order-dto>
  27. </root>
with attributeFormDefault = Qualified
and elementFormDefault = UnQualified:

Expand|Select|Wrap|Line Numbers
  1. <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.company.net/product">
  2.   <partner-dto d2p1:message-status="I" xmlns:d2p1="http://www.company.net/product" xmlns="">
  3.     <internal-reference />
  4.     <external-reference>1564</external-reference>
  5.     <functional-reference>1564 Naamklant</functional-reference>
  6.     <description>Naamklant</description>
  7.     <allow-partial-deliveries>true</allow-partial-deliveries>
  8.     <main-address-reference />
  9.     <state-reference />
  10.   </partner-dto>
  11.   <partner-dto d2p1:message-status="I" xmlns:d2p1="http://www.company.net/product" xmlns="">
  12.     <internal-reference />
  13.     <external-reference>1564</external-reference>
  14.     <functional-reference>1564 Naamklant</functional-reference>
  15.     <description>Naamklant</description>
  16.     <allow-partial-deliveries>true</allow-partial-deliveries>
  17.     <main-address-reference />
  18.     <state-reference />
  19.   </partner-dto>
  20.   <sales-order-dto d2p1:message-status="I" xmlns:d2p1="http://www.company.net/product" xmlns="">
  21.     <delivery-address-reference xsi:nil="true" />
  22.     <ship-together>true</ship-together>
  23.     <sales-reference />
  24.     <order-date>0001-01-01T00:00:00</order-date>
  25.   </sales-order-dto>
  26. </root>
With attributeFormDefault = Qualified
and elementFormDefault = Qualified:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.company.net/product">
  3.   <partner-dto d2p1:message-status="I" xmlns:d2p1="http://www.company.net/product">
  4.     <d2p1:internal-reference />
  5.     <d2p1:external-reference>1564</d2p1:external-reference>
  6.     <d2p1:functional-reference>1564 Naamklant</d2p1:functional-reference>
  7.     <d2p1:description>Naamklant</d2p1:description>
  8.     <d2p1:allow-partial-deliveries>true</d2p1:allow-partial-deliveries>
  9.     <d2p1:main-address-reference />
  10.     <d2p1:state-reference />
  11.   </partner-dto>
  12.   <partner-dto d2p1:message-status="I" xmlns:d2p1="http://www.company.net/product">
  13.     <d2p1:internal-reference />
  14.     <d2p1:external-reference>1564</d2p1:external-reference>
  15.     <d2p1:functional-reference>1564 Naamklant</d2p1:functional-reference>
  16.     <d2p1:description>Naamklant</d2p1:description>
  17.     <d2p1:allow-partial-deliveries>true</d2p1:allow-partial-deliveries>
  18.     <d2p1:main-address-reference />
  19.     <d2p1:state-reference />
  20.   </partner-dto>
  21.   <sales-order-dto d2p1:message-status="I" xmlns:d2p1="http://www.company.net/product">
  22.     <d2p1:delivery-address-reference xsi:nil="true" />
  23.     <d2p1:ship-together>true</d2p1:ship-together>
  24.     <d2p1:sales-reference />
  25.     <d2p1:order-date>0001-01-01T00:00:00</d2p1:order-date>
  26.   </sales-order-dto>
  27. </root>
That last result seems like the best solution, namespace wise, no discussion.

But I think the first version is more "readable" (no qualifiers), only problem is that xmlns="" tag. If that's invalid xml then I wonder the use of setting elementFormDefault=UnQualified in the first place.

I'll post any progress in this topic.

Regards,

Boris
Aug 30 '07 #3

P: 3
Hmmm, the other company sais that XSD is valid and it's our job to fill in the xmlns tag correctly...

So if anyone has a suggestion on how to do this or explain me why this can't be done, I'd be very impressed.

The only 'hack' I can think of is removing the xmlns="" tag through String.Replace from the output.

Regards,

Boris
Sep 4 '07 #4

jkmyoung
Expert 100+
P: 2,057
If the XSD is valid, then output xml you have originally is valid.

Part of your xml just happens to be in the default namespace. This is still valid xml.
Sep 5 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.