0

I want to use a namespace in this format "name1:name2:name3", but when I try to use it in my vb class like this

Namespace name1:name2:name3

Partial Public Class Message
End class

End namespave

I get an error: Declaration expected.

I have no control over the namespace so I can not change it.

Thanks in advance!

EDIT

I use xsd.exe to autogenerate a class fromn an xsd. I use the n switch to set the namespace. I get the namespace from the organization who provides the xsd and the namespace is on the form "ukm:sst:collection:detail". I have to use it in my class to get the xml i serialize from the class validated.

My xml should look like this

<?xml version="1.0" encoding="UTF-8" ?> 
<message xmlns="ukm:sst:collection:detail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xsi:schemaLocation="ukm:sst:collection:detail">

EDIT2

My class:

<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929"), _
 System.SerializableAttribute(), _
 System.Diagnostics.DebuggerStepThroughAttribute(), _
 System.ComponentModel.DesignerCategoryAttribute("code"), _
 System.Xml.Serialization.XmlTypeAttribute([Namespace]:="ukm:sst:collection:detail"), _
 System.Xml.Serialization.XmlRootAttribute("melding", [Namespace]:="ukm:sst:collection:detail", IsNullable:=False)> _
Partial Public Class Message

But my xml is missing the xsi:schemaLocation attribute

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="ukm:sst:collection:detail">

but the xml should look like:

<?xml version="1.0" encoding="UTF-8" ?> 
<message xmlns="ukm:sst:collection:detail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xsi:schemaLocation="ukm:sst:collection:detail test_v2_0.xsd">

I have the Imports System.Xml.Serialization in my autogenerated class.

What is it I'm doing wrong?

Liss
  • 95
  • 1
  • 3
  • 11
  • possible duplicate of [.NET Multiple Namespaces for single class](http://stackoverflow.com/questions/3805460/net-multiple-namespaces-for-single-class) – Sergi Jan 09 '14 at 09:45
  • You are not using the `/n` option correctly. Pick a name, any name, use a period instead of `:`. – Hans Passant Jan 09 '14 at 10:29

2 Answers2

1

I think you are confusing the VB.Net Class Namespace, with the Xml namespace. The two are not one and the same thing.

If you want a namespace in your Xml Output from the class, you should use the XmlRoot Attribute, and specify the namespace in there:

<XmlRoot(Namespace:="ukm:sst:collection:detail")>
Public Class Message

End Class

NB: You will want to import System.Xml.Serialization in your class page.

Based on your Edit2, In order to get SchemaLocation in, it appears that you need to add a property to the class that contains it, and specify it using XmlAttribute (Found from this answer):

<XmlRoot(Namespace:="ukm:sst:collection:detail")>
Public Class Message

  <XmlAttribute("schemalocation", Namespace:=XmlSchema.InstanceNamespace)>
  public string SchemaLocation = "ukm:sst:collection:detail test_v2_0.xsd"

End Class
Community
  • 1
  • 1
Obsidian Phoenix
  • 4,083
  • 1
  • 22
  • 60
  • Updated the answer with the (apparent) solution – Obsidian Phoenix Jan 09 '14 at 10:59
  • Thanks again!! This is the answer I was looking for! But what confuses me is that the vb file is autogenerated, and I belived that alle the information nessesary was provided from the xsd the vb code vase generated from. It is not recomended to alter the autogen vb file so is it possible to put the code you provided in a patial extention file for may class message? – Liss Jan 09 '14 at 11:07
  • Possibly, yes. Give it a try and see. Just create a partial class in the same namespace and assembly. – Obsidian Phoenix Jan 09 '14 at 11:08
  • Thanks again for all your help!! This was the solution, It worked fine to put it in the partial extention file. – Liss Jan 09 '14 at 11:53
1

You can't have comma (,) or colon (:) in VB.NET Namespace. As stated in MSDN that Namespace should be :

"A unique name that identifies the namespace. Must be a valid Visual Basic identifier"

And a valid Visual Basic identifier must not contain such characters :

"It must only contain alphabetic characters, decimal digits, and underscores." [Reference]

har07
  • 88,338
  • 12
  • 84
  • 137