469,306 Members | 1,903 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Modifying namespace in XML doc, C# help

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.

************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Nov 12 '05 #1
5 13379


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/

Nov 12 '05 #2
Hi Thanks for the response, I've tried some similar things.
However when I run your suggested code, it runs without error, but the XML file is not updated. I stays the same as it was.
I tried moving the file around, and specifying @"c:\test.xml", it runs fine, when I open the XML document it hasn't been changed at all.
If I delete test.xml it throws an error saying it couldn't find test.xml, so obviously its finding the document but its not modifying the attributes.
Is this because I'm trying to modify the attribute of the document element?
Nothing I've tried has worked. I've checked all permissions on the text file and its not that.
I'm using .NET 2003 although that should not make a difference
************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Nov 12 '05 #3
Never mind the last posting....
The console was dissapearing and I wasn't reading it properly so i specified
Console.Read()
and the output is being correctly sent.
I should have qualified my question better (i'm just starting XML programming in .NET).
My question then is, is it posible to overwrite the original Text.xml file?
If not how how do you create the new xmlDocument into an xmlDocument file, not just output written to the console?
Thanks again

************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Nov 12 '05 #4
Never Mind again,
I used the XMLDocument.Save(@"c:\NewTest.xml)

I'm working out of "XML How to Program" Deitel and "XML for .NET Developers" Wrox.
And I've been working on this problem for 2 days now and none of my resources have what you have written down.

Thank you very much.

Also, could you please let me know where you got this information, you probably learned the hardway, or if you have a good book that contains this stuff I'd love to know what it is.

Again Thank you very very much.
************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Nov 12 '05 #5


jr****@hotmail.com wrote:

However when I run your suggested code, it runs without error, but
the XML file is not updated. I stays the same as it was.


Well, yes, the example only outputs the changed XML to the console, if
need you can save the document to a file with
xmlDocument.Save(@"whatever.xml")
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.