0

Im working on an project where I use an autogenereted vb class. I want to serialize this class to an xml file. When I validate my xml file against my xsd fil I get an error: Cannot Find The Declaration Of Element 'message'.

My xsd looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema elementFormDefault="qualified" attributeFormDefault="unqualified" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
xmlns:tera="http://tera.net/xsd/data"
xmlns:ns1="http://tera.net/xsd/Company-dok/note/2010"><xsd:element name="Message" 
type="TestMessage" /><xsd:complexType      
name="TestMessage"><xsd:sequence><xsd:element name="Detail" type="Detail" /> ........

My xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:ska:test:micro:v2">

But it should look like this:

<?xml version="1.0" encoding="UTF-8" ?> 
<message xmlns="urn:ska:test:micro:v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xsi:schemaLocation="urn:ska:test:micro:v2 test_v2_0.xsd">

What am I missing?

When I serialize my class to xml I want my elemts properties to appeare in my xml in a given order. This can I solve if I cange my autogenerated file. If I cange the order of the properties in the classes in the autogenerated file the atributes in the xml will appear in the correct order. But I do not want to change my autogenerated file, what can I do?

Part of my autogen file:

<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="urn:ska:test:micro:v2")> _
Partial Public Class Detail

Private NameField As String

Private PhoneField() As Oppgavegiver


'''<remarks/>
Public Property name() As String

End Property



'''<remarks/>
Public Property phone() As integer

End Property

If I place Phonefield on top of nameField and phone property on top of name property I get the correct order in my xml, but I do not want to change the sutogenerated file.

What do I have to do?

Thanks in advance!

Liss
  • 95
  • 1
  • 3
  • 11

1 Answers1

0

The error implies that for the XML you're validating, the message doesn't have a namespace, which matches the XSD you're showing (since it doesn't have a targetNamespace). However, the first posted XML places the message element into the urn:ska:test:micro:v2 namespace through xmlns="urn:ska:test:micro:v2" which is totally confusing, since your XSD doesn't have a targetNamespace. Something got lost in "translation", so you should check your posting.

Otherwise, you seem to be asking two things:

(1) How to add a schemaLocation attribute to your generated XML. The answers to this post of SO should provide you with enough information on how to do it (custom serializer or a partial class).

(2) How to ensure that particles are properly ordered. The answer is use the /order switch in your xsd.exe command line.

Community
  • 1
  • 1
Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
  • Thanks a lot! I tried the order switch, but when I look at my class I can see that my properties have the wrong order number, how can I change the order without edit the autogen file? I have a example xml file that shows how the order shoud be, and my xml must have the same order as the example file. – Liss Jan 08 '14 at 22:55
  • @Liss, you have to make sure that your XSD reflects the order in which the XML is authored. If you're using xsd:all, then there is no way to change that, and you shouldn't care either. If you need to, then change the xsd:all to an xsd:sequence. The caveat though might be that you're looking at one particular XML, where other XMLs might be different. I would suggest you first ensure your XSD matches your XML. – Petru Gardea Jan 08 '14 at 23:17
  • Thanks again. I have no control over the xsd. This is a xsd third parties developers (like me) can use to generate an xml that is in the same way as my example xml – Liss Jan 08 '14 at 23:28
  • I think that you may be constraining yourself too much... is the XML created by your program valid as per the XSD? If yes, are there any other constraints documented outside of the XSD that you're not following, and where the order is stipulated as relevant? – Petru Gardea Jan 08 '14 at 23:48
  • when I vaidate my xml against the xsd i get error messages on the properties when they are in the "wrong" order but when I change the order and generate a new xml it validates correct against the xsd. But the message element is still wrong. Sorry for not understanding your answer but if my xml is based on my class generated from the xsd why is the xsi:schemaLocation missing? I thougt the Message elemnt and its atributes would appear automatically. Thanks again! – Liss Jan 09 '14 at 00:22
  • First of all, you shouldn't be expecting an xsi:schemaLocation attribute to be generated... Think about it, where do you see it in the provided XSD as part of the message definition? Based on what you shared, I can't really help you more unless you can share the full XSD and XML (both given and expected)... – Petru Gardea Jan 09 '14 at 03:02