473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 13564


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: lallous | last post by:
file: prog1.cpp #include <stdio.h> #include "temp1.h" int main() { double d; Templ1::func(d); return 0; }
1
by: Roberto Modica | last post by:
Hi all, I am trying to create a namespace hierachy but i think i am doing something wrong. I have an DAL vb file with the namespace: CompanyName.newsletter.DAL and i compile that using...
1
by: The Real Andy | last post by:
This is probably not the most relevant NG for this topic, but if someone could point me to a more relevant NG I would be forever grateful. I am having some issues with the Managed DirectX...
14
by: Tiraman | last post by:
Hi , I would like to use nested namespace . I have 3 namespace as dll's : Namespace A Namespace B Namespace C And i want to have some namespace that contain them all , some thing like
0
by: john | last post by:
I know that windows CE has a SQLCE client namespace but i do not want to use it because it needs to download my sql tables to a local sql database on the CE.NET client machine. I would rather have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.