jr****@hotmail.com wrote:
Hi I have a document as follows:
<?xml version="1.0"?> <metadata xml:lang="en"> </metadata>
I want to change the document so it looks as follows:
<?xml version="1.0"?> <metadata
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://localhost/XMLDemo/MyXMLschema.xsd">
</metadata>
For the life of me I cannot figure out how to do this. I can't find
anything in any of the C# XML classes since a namespace cannot be a
child, I cannot replacechild, etc. I'm completely stumped. Any help
would be appreciated.
Your subject line asks about modifying a namespace however your example
only removes the xml:lang attribute and adds two attributes (which have
a qualified name), that is possible for instance with DOM programming,
here is an example C# program that does the change
using System;
using System.Xml;
public class Test20040216 {
public static void Main (string[] args) {
string fileName = @"test20040216.xml";
string xsiNS = "http://www.w3.org/2001/XMLSchema-instance";
string xmlnsNS = "http://www.w3.org/2000/xmlns/";
string xmlNS = "http://www.w3.org/XML/1998/namespace";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(fileName);
XmlAttribute attributeNode = xmlDocument.CreateAttribute("xmlns",
"xsi", xmlnsNS);
attributeNode.Value = xsiNS;
xmlDocument.DocumentElement.SetAttributeNode(attri buteNode);
attributeNode = xmlDocument.CreateAttribute("xsi",
"noNamespaceSchemaLocation", xsiNS);
attributeNode.Value = "http://localhost/XMLDemo/MyXMLschema.xsd";
xmlDocument.DocumentElement.SetAttributeNode(attri buteNode);
xmlDocument.DocumentElement.RemoveAttribute("lang" , xmlNS);
Console.WriteLine(xmlDocument.OuterXml);
}
}
--
Martin Honnen
http://JavaScript.FAQTs.com/