Hi,
I have the following problem:
I am using vb.net (visual studio 2008)
I want to add a piece of xml to an existing xml file.
The existing file has a default namespace.
This is a part of the xml file I wish to change:
<?xml version="1.0" encoding="utf-8" ?>
<db xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.lalala.be.db" xsi:schemaLocation="http://www.lalala.be/db db.xsd" >
<keyboard>
<department>23</department>
<keyboardid>1000</keyboardid>
</keyboard>
</db>
This is the significant part of the code:
'I import here the namespace of the original xml file so I can use the intellisense
Imports <xmlns:ns="http://www.lalala.be/db">
Private xdoc As XDocument
Private root As XElement
Private myreader as xmlreader
myreader = XmlReader.Create("db.xml")
xdoc = XDocument.Load(myreader)
root = xdoc.Root
Dim tempelement As XElement = _
<lala>
<hola>bleh</hola>
</lala>
root.Add(tempelement)
When I execute this, the document contains:
<db xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.lalala.be/db" xsi:schemaLocation="http://www.lalala.be/db db.xsd">
- <keyboard>
<department>23</department>
<keyboardid>1000</keyboardid>
</keyboard>
- <lala xmlns="">
<hola>bleh</hola>
</lala>
</db>
Please notice the xmlns="" namespacetag that gets added to the newly added element
This causes the xml document to be no longer valid compaired to the schema!
When I add the namespace to the tempelement like this,
Dim tempelement As XElement = _
<ns:lala>
<ns:hola>bleh</ns:hola>
</ns:lala>
Then I get the following resuling xml file:
- <db xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.lalala.be/db" xsi:schemaLocation="http://www.lalala.be/db db.xsd">
- <keyboard>
<department>23</department>
<keyboardid>1000</keyboardid>
</keyboard>
- <ns:lala xmlns:ns="http://www.lalala.be/db">
<ns:hola>bleh</ns:hola>
</ns:lala>
</db>
Again notice the newly added tag, though I used the same namespace as the default one of the document,
It does not do what I expected it to do.
This also does not pass schema validation :-/
I would like to get rid of the xml namespace prefix and name in the newly added xelement.
This is the output I would like to get.
- <db xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.lalala.be/db" xsi:schemaLocation="http://www.lalala.be/db db.xsd">
- <keyboard>
<department>23</department>
<keyboardid>1000</keyboardid>
</keyboard>
- <lala>
<hola>bleh</hola>
</lala>
</db>
So i would like to add new xml tags to my xml file, that uses the default namespace of the original document.
I do not want any new extra namespace data to be added to the new xelement data that I add to my document.
I am sure I am doing something wrong, I just do not get what.
Greetings,
Nirious
I have the following problem:
I am using vb.net (visual studio 2008)
I want to add a piece of xml to an existing xml file.
The existing file has a default namespace.
This is a part of the xml file I wish to change:
<?xml version="1.0" encoding="utf-8" ?>
<db xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.lalala.be.db" xsi:schemaLocation="http://www.lalala.be/db db.xsd" >
<keyboard>
<department>23</department>
<keyboardid>1000</keyboardid>
</keyboard>
</db>
This is the significant part of the code:
'I import here the namespace of the original xml file so I can use the intellisense
Imports <xmlns:ns="http://www.lalala.be/db">
Private xdoc As XDocument
Private root As XElement
Private myreader as xmlreader
myreader = XmlReader.Create("db.xml")
xdoc = XDocument.Load(myreader)
root = xdoc.Root
Dim tempelement As XElement = _
<lala>
<hola>bleh</hola>
</lala>
root.Add(tempelement)
When I execute this, the document contains:
<db xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.lalala.be/db" xsi:schemaLocation="http://www.lalala.be/db db.xsd">
- <keyboard>
<department>23</department>
<keyboardid>1000</keyboardid>
</keyboard>
- <lala xmlns="">
<hola>bleh</hola>
</lala>
</db>
Please notice the xmlns="" namespacetag that gets added to the newly added element
This causes the xml document to be no longer valid compaired to the schema!
When I add the namespace to the tempelement like this,
Dim tempelement As XElement = _
<ns:lala>
<ns:hola>bleh</ns:hola>
</ns:lala>
Then I get the following resuling xml file:
- <db xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.lalala.be/db" xsi:schemaLocation="http://www.lalala.be/db db.xsd">
- <keyboard>
<department>23</department>
<keyboardid>1000</keyboardid>
</keyboard>
- <ns:lala xmlns:ns="http://www.lalala.be/db">
<ns:hola>bleh</ns:hola>
</ns:lala>
</db>
Again notice the newly added tag, though I used the same namespace as the default one of the document,
It does not do what I expected it to do.
This also does not pass schema validation :-/
I would like to get rid of the xml namespace prefix and name in the newly added xelement.
This is the output I would like to get.
- <db xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.lalala.be/db" xsi:schemaLocation="http://www.lalala.be/db db.xsd">
- <keyboard>
<department>23</department>
<keyboardid>1000</keyboardid>
</keyboard>
- <lala>
<hola>bleh</hola>
</lala>
</db>
So i would like to add new xml tags to my xml file, that uses the default namespace of the original document.
I do not want any new extra namespace data to be added to the new xelement data that I add to my document.
I am sure I am doing something wrong, I just do not get what.
Greetings,
Nirious
Last edited: