setting an XmlElement its prefix.
Hi,
I would like to add an XmlElement that looks like this:
<data:lvalue-possibles>
Here is my current code.
Code:
XmlDocument root = new XmlDocument();
XmlNode attr = root.ImportNode(base.Xml,true);
root.AppendChild(attr);
XmlElement possibles = root.CreateElement("data:lvalue-possibles");
possibles.Prefix = "data";
attr.AppendChild(possibles);
return attr;//returns the XmlNode
This code is part of a function which returns an XMLNode. It also overrides a function of another class. It takes the XMLNode returned by that baseclass function and just adds the new tag.
Here's some explanations of the code above:
1) To create an XmlElement there is always an XmlDocument needed. So first I create the XmlDocument.
Code:
XmlDocument root = new XmlDocument();
2) As I said earlier the function overrides another function. First we'll need to convert the tags of the basefunction to the newly created document.
Code:
XmlNode attr = root.ImportNode(base.Xml,true);
3) Here we make those tags the root of our document.
Code:
root.AppendChild(attr);
4) Next we create the new XMLElement.
Code:
XmlElement possibles = root.CreateElement("data:lvalue-possibles");
5) I am trying really hard to set the prefix to "data:...".
Code:
possibles.Prefix = "data";
6) After that the new element is added to the previous one.
Code:
attr.AppendChild(possibles);
I have no idea why, but this does not work. Everything works fine that is, except the prefix. No matter what I try the prefix does not show up. The output is this:
Quote:
<baseclasstags>
<lvalue-possibles />
</baseclasstags>
When I debug I can see that even right after the 4th step there is no prefix in the InnerXML property. So it looks like it was never added.
Does anybody know how I can set the prefix anyway?
Re: setting an XmlElement its prefix.
I read somewhere that I need to add a namespacemanager. Tried it but made no difference:
Code:
XmlNamespaceManager n = new XmlNamespaceManager(root.NameTable);
n.AddNamespace("data", "http://lamp.cfar.umd.edu/viperdata");
Re: setting an XmlElement its prefix.
The prefix is called a namespace. In the W3C DOM you use the createElementNS() method to add an element using a qualified namespace. In the .NET class it is implemented using an overload of CreateElement.
http://msdn2.microsoft.com/en-us/lib...teelement.aspx
Code:
XmlElement possibles = root.CreateElement(
"lvalue-possibles",
"data",
"http://lamp.cfar.umd.edu/viperdata"
);
Re: setting an XmlElement its prefix.
Thank you :) seems like it did indeed work like that !