XML Saving is Adding Namespace field

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Well,

I've noticed a little something amusing in the VB Default XML handling and I was wondering how to eradicate this behavior.

I've designed an XML Schema (which is built in to my resources and provided for verification), and i' utilize a dataset to store the values from the XML document while in memory, because datasets are rather easy to use and they come built in with relationship enforcement which can be helpful.

However, their default save/load is not as desired so i wrote my own, utilizing my knowledge of XML, and the XML vb wrapper library. So,
I create the document adding elements and attributes as necessary and this is what I get:
VB.NET:
<?xml version="1.0" encoding="utf-8"?>
<RexamSettings xmlns="http://tempuri.org/RexamSettings.xsd">
  <usersettings ID="1" user_name="sk" ua="False" xmlns="">
  </usersettings>
</RexamSettings>

Seems okay, never fails on the schema validation, never causes any hiccups, but the VB IDE Intellisense reads that as the usersettings having a warning with regards to the RexamSettings.xsd schema file.

I remove the usersettings attribute section: xmlns="" and the warning goes away. NOw, when I create that element as a child to the RexamSettings "DocumentElement", I do not provide a NameSpaceURI, but for some reason when it comes time to save the document to the xmlwriter, it adds a "blank" Namespace attribute to the element. How can I stop this behavior?

Thanks
 
Set same namespace for usersettings nodes as the RexamSettings node. When working with namespaces a XmlNamespaceManager is convenient, adding a namespace with no prefix sets the DefaultNamespace.
You're using XmlDocument? XmlWriter may be easier if generating a full document.
 
Set same namespace for usersettings nodes as the RexamSettings node. When working with namespaces a XmlNamespaceManager is convenient, adding a namespace with no prefix sets the DefaultNamespace.
You're using XmlDocument? XmlWriter may be easier if generating a full document.

I have an extended XmlDocument and XmlElement for easier creation and manipulation. And I use the XmlWriter to save it to file. I just use the XmlDocument part for creation of elements/attributes, as well as for the loading part reading of elements/attributes.

Basically, I guess I need to look into the NameSpace Manager.

I found that basically every AddElement() call i Make (one of my extensions that makes the creation of XmlNOde.ChildNode (of type element) easier) i had to add the namespaceURI param. If I include the NamespaceURI it doesn't do that = "" but other wise it does. Guess i will look into the manager.
 
Easier said than done, looking into the NamespaceManager.

Any Suggested references for how to use it with regards to XmlDocument/XmlWriter?

I understand the document aspect, but how to link them together seems tricky and the default help documentation is not very expressive, *chuckle*

Thanks
 
So you're building the document through the XmlDocument interface, you still have to set the namespace for all nodes that should belong to a uri when the node is created, but using XmlNamespaceManager puts that into a better managable order of things.
VB.NET:
Dim xdoc As New XmlDocument
Dim ns As New XmlNamespaceManager(xdoc.NameTable)
ns.AddNamespace("", "http://tempuri.org/RexamSettings.xsd")
Dim root As XmlElement = xdoc.CreateElement("root", ns.DefaultNamespace)
Dim child As XmlElement = xdoc.CreateElement("child", ns.DefaultNamespace) 
root.AppendChild(child)
xdoc.AppendChild(root)
 
I see. So it's just a better management system, but I'll still need to add it to each element. I just set a class wide constant, but I can see how this is better for multi-namespace management.

Thanks
 
Back
Top