BizTalk Benjamin wrote:
Hi, I have an XmlDocument loaded from a memory stream. I set the document
element prefix in this way
XmlElement e = xDoc.DocumentElement;
e.Prefix = "abc"
When i simply write the document element to the command line it shows the
prefix. However, it does not persist in the XmlDoc so when i save the XmlDoc
into a file, the prefix is no longer there.
Can anyone help? I need to sort this out rather urgently.
What exactly do you want to achieve? Once a node has been created you
can't change its namespace.
See the documentation of prefix
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlElementClassPrefixTopic.asp>
which says
"Setting this property changes the Name property, which holds the
qualified name for an XmlElement. However, changing the prefix does not
change the namespace URI of the element."
So you can change the prefix if you want but the namespace of the
element does not change, e.g.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(
"<g:gods xmlns:g=\"http://example.com/2006/gods\" />");
xmlDocument.Save(Console.Out);
xmlDocument.DocumentElement.Prefix = "h";
xmlDocument.Save(Console.Out);
outputs
<g:gods xmlns:g="http://example.com/2006/gods" />
for the first Save call and
<h:gods xmlns:g="http://example.com/2006/gods"
xmlns:h="http://example.com/2006/gods" />
for the second Save call.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/