472,805 Members | 965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 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 13534


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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.