473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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
4 4788
jkmyoung
2,057 Expert 2GB
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
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
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
2,057 Expert 2GB
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

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

Similar topics

3
by: Mike Dickens | last post by:
hi, i'm sure this has come up before but havn't managed to find an answer. if i have the following xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet method="xml" version="1.0"...
3
by: Keith Hill | last post by:
I am creating an XmlDocument in code and then using XmlTextWriter via doc.WriteTo(xwriter) to output the result to a text box. I have a root element that defines a default namespace. However, the...
9
by: Anubhav Jain | last post by:
Hi, I am having few .net source files(.cs or .vb) and I want to dynamically generate the corresponding .net project file(.csproj or .vbproj) for them without using visual studio.So that I could...
3
by: olympus_mons | last post by:
Hi all, my XML looks like: <root xmlns="tempuri.org"> <result xmlns=""> <date>2006-09-14</date> <status>ok</status> </result> </root>
0
by: Peter Nofelt | last post by:
Hi all, ISSUE: ==================== In SQL 2005 (sp2) I get the following error when preforming a bulk insert with an associated xml format file: "Could not bulk insert. Unknown version of...
3
by: Lee | last post by:
Hello guys, I am new to XML and working on a XSLT to transforn yahoo shopping search result to html. my problem is the return XML contain xmlns in root element, here is the sample xml: <?xml...
7
by: Tim Slattery | last post by:
I'm trying to handle the onChange event in an <input type="file"> element. In IE, there's no problem: the event fires when a file in the "open" box is doubleclicked, or the "Open" button in the box...
1
by: niharnayak2003 | last post by:
Hi All, I am facing the problem in Typecast inside the Generic class. I need a function which will take two param 1:-Xpath 2:- XMLDOC and return me what i will need. here is the code for...
36
by: Roedy Green | last post by:
The only browser I have encountered that supports <colgroup><col class="behold"></colgroup> to apply a CSS style to a whole column, is Microsoft Internet Explorer. I have been told it SHOULD NOT...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.