XML Literals in VB9 - What's wring with this?

  • Thread starter Thread starter Rory Becker
  • Start date Start date
R

Rory Becker

The following code compiles fine in VB9 but the msgbox is empty Why?

-------------------------------------------------------------
Public Class Foo
Public Shared Sub DoSomething()
Dim x = <library>
<shelf>
<book id="1" name="Some Book"/>
</shelf>
</library>
Call System.Windows.Forms.MessageBox.Show(x.<library>.<shelf>.<book>.@Name)
End Sub
End Class
 
Rory Becker said:
The following code compiles fine in VB9 but the msgbox is empty Why?

-------------------------------------------------------------
Public Class Foo
Public Shared Sub DoSomething()
Dim x = <library>
<shelf>
<book id="1" name="Some Book"/>
</shelf>
</library>
Call
System.Windows.Forms.MessageBox.Show(x.<library>.<shelf>.<book>.@Name)

Try '@name' instead of '@Name'. XML attribute names are case-sensitive.
 
Hello Herfried K. Wagner [MVP],
Public Class Foo
Public Shared Sub DoSomething()
Dim x = <library>
<shelf>
<book id="1" name="Some Book"/>
</shelf>
</library>
Call
System.Windows.Forms.MessageBox.Show(x.<library>.<shelf>.<book>.@Name)

Unfortunately I have spotted that particular bug and tried since posting.

Still nothing I'm afraid.

Thanks for the reply though.
 
Rory said:
Hello Herfried K. Wagner [MVP],


Unfortunately I have spotted that particular bug and tried since posting.
Still nothing I'm afraid.

You need
x.<shelf>.<book>.@name
as x (in your code sample) is an XElement (the library element) and not
an XDocument.

Your expression would work if you had an XDocument containing the
library element as the root element.
 
Martin said:
Your expression would work if you had an XDocument containing the
library element as the root element.

To create an XDocument you would need e.g.

Dim x As XDocument = <?xml version="1.0"?>
<library>
<shelf>
<book id="1" name="Some Book"/>
</shelf>
</library>
 
Hello Martin,
To create an XDocument you would need e.g.

Dim x As XDocument = <?xml version="1.0"?>
<library>
<shelf>
<book id="1" name="Some Book"/>
</shelf>
</library>


Thanks very much for your help here Martin.
Spot on and your follow up point in this post is also appriciated.

I have a further problem which you may be able to help with.

I have some incoming XML which I cannot directly control.
I have locally created a schema for it and imported it for the moment as
the default xmlns.
However the incoming file has no such namespace listed in it.

What is the best was to add the namespace to the incoming document so that
my XMl Queries will match?

At the moment I am injecting the namespace directly into the incoming root
node but this seems a little OTT.
 
Rory said:
I have some incoming XML which I cannot directly control.
I have locally created a schema for it and imported it for the moment as
the default xmlns.
However the incoming file has no such namespace listed in it.

What is the best was to add the namespace to the incoming document so
that my XMl Queries will match?

At the moment I am injecting the namespace directly into the incoming
root node but this seems a little OTT.

Schema and namespace are two different issues, you can write a schema
for elements in no namespace if needed. So I am not sure I understand
the problem, why do you want to use a namespace if the incoming XML does
not have elements in a namespace?
 
Hello Martin,
Schema and namespace are two different issues, you can write a schema
for elements in no namespace if needed. So I am not sure I understand
the problem, why do you want to use a namespace if the incoming XML
does not have elements in a namespace?

Ah so I have found someone who knows what they're talking about.

Excellent.

Ok so I have XMl coming in that looks like this....

-------------------------------------------------------------
<Element1>
<Element2>
<Element3>

</Element3>
</Element2>
</Element1>
-------------------------------------------------------------
I have used this to generate a schema so that I could access this through
VB's XMlLiterals using Intellisense.

-------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Element1">
<xs:complexType>
<xs:sequence>
<xs:element name="Element2">
<xs:complexType>
<xs:sequence>
<xs:element name="Element3" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
-------------------------------------------------------------

I began to explore this and discovered a targetNamespace attribute was available
in the <xs:schema> element which tied in with what was presented to me in
VB for imports options.

I created such an attribute on my schema
-------------------------------------------------------------
<xs:schema targetNamespace="http://SomeNameSpace" attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
-------------------------------------------------------------

Then I imported the namespace in my VB Project thus
-------------------------------------------------------------
Imports <xmlns="http://SomeNameSpace">
-------------------------------------------------------------

However when I used an XElement to Parse my data in from it's source I found
that basic Queries failed.

This seemed to be corrected if I either removed my imports statement or injected
an...
-------------------------------------------------------------
xmlns="http://SomeNameSpace"
-------------------------------------------------------------
....attribute into my data.

Now I freely admit to being confused by a fair amount of XML stuff and have
learned all of this through experimentation, so I may well have got some
of the terminology wrong. Sorry about that. :)

Hopefully this clarrifies my position.

So therefore my question is how can I retain my Intellisense while coding
queries and not have to "Inject" the xmlns attribute directly into my text?

Thanks once again for your help
 
Rory said:
So therefore my question is how can I retain my Intellisense while
coding queries and not have to "Inject" the xmlns attribute directly
into my text?

The documentation on XML intellisense focusses on documents with
namespaces but I have tried a simple example here where I have written a
schema with no targetNamespace and added that schema to my VB.NET
project. Then I got intellisense for an XDocument I loaded from a file.
So give that a try, generate your schema and add it to the project, that
way I think you should get intellisense.
 
Hello Martin,
The documentation on XML intellisense focusses on documents with
namespaces but I have tried a simple example here where I have written
a schema with no targetNamespace and added that schema to my VB.NET
project. Then I got intellisense for an XDocument I loaded from a
file.
So give that a try, generate your schema and add it to the
project, that way I think you should get intellisense.

Ok that seems to work.

The only thing is that that if I have multiple xsds in my project then I
get the elements for both suggested. This is not a real problem until my
schema gets a bit more complicated.

Unfortunatly this is likely going to happen pretty soon.

I guess I'm going to have to stick with my manual injection for now.
 
Hi Rory,

As for generating xsd scheme, .net framework does provide a tool "xsd.exe",
it can help you generate xsd from a given sample XML document or generate
.net class(for xml serialization) from a given xsd schem:

#XML Schema Definition Tool (Xsd.exe)
http://msdn2.microsoft.com/en-us/library/x6c1kb0s(VS.71).aspx

However, since it's code logic is quite simple, the generated schema or
class may not 100% match what you need. I think it can be a utility that
can help you generate a draft schema and you further edit it(if the one you
want to generate from is quite complex).

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Date: Mon, 18 Feb 2008 23:15:14 +0000 (UTC)
Message-ID: <[email protected]>
From: Rory Becker <[email protected]>
Subject: Re: XML Literals in VB9 - What's wring with this?
 
Back
Top