473,378 Members | 1,397 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,378 software developers and data experts.

Global namespace URI replacement

Hello all,

I am trying to globally replace all namespace URIs in an XML document.
I have tried using XmlNamespaceManager to do this, but have not had any
success. I don't want to do a simple string replacement on the
original text, as the URI may be referenced in a text node, which I
would not want to replace. Short of regenerating the document node by
node, is there any way to do this?

Thanks!

Chris Bray

Nov 12 '05 #1
4 1480
Chris Bray wrote:
I am trying to globally replace all namespace URIs in an XML document.
I have tried using XmlNamespaceManager to do this, but have not had any
success. I don't want to do a simple string replacement on the
original text, as the URI may be referenced in a text node, which I
would not want to replace. Short of regenerating the document node by
node, is there any way to do this?


1. Custom XmlReader, which exposes different namespace - the most
efficient way.
2. Custom XmlWriter, which writes XML with different namespace - simpler
to implement than XmlReader, but requires to write XML down to bytes.
3. XSLT - the simplest way, not necessarily the best perf.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2
Thanks! I'll give those a try.

Nov 12 '05 #3
Chris Bray wrote:
Thanks! I'll give those a try.


Don't hesitate to ask here for any additional help on that.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #4
Oleg,

Using a custom XmlReader worked great. Here is my code for anyone who
has the same problem:

/// <summary>
/// Summary description for XmlSubstitutionReader.
/// </summary>
public class XmlSubstitutionReader : XmlTextReader
{
#region Fields

private Hashtable pSubstitutions;

#endregion

#region Constructors

public XmlSubstitutionReader(Stream manifest) : base(manifest)
{
this.pSubstitutions = new Hashtable();
}

#endregion

#region AddSubstitution

public void AddSubstitution(string oldValue, string newValue)
{
if (this.pSubstitutions.ContainsKey(oldValue))
{
throw new ApplicationException("The provided value is already
configured for substitution.");
}
else
{
this.pSubstitutions.Add(oldValue, newValue);
}
}

#endregion

#region DoSubstitutions

private string DoSubstitutions(string value)
{
string v = value;

if ((value != null) && (value != string.Empty))
{
foreach (string s in this.pSubstitutions.Keys)
{
v = v.Replace(s, (string) this.pSubstitutions[s]);
}
}

return v;
}

#endregion

#region NamespaceURI

public override string NamespaceURI
{
get
{
return this.DoSubstitutions(base.NamespaceURI);
}
}

#endregion

#region Value

public override string Value
{
get
{
return this.DoSubstitutions(base.Value);
}
}

#endregion

Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
12
by: Santiago de Compostela | last post by:
Hi The following program doesn't compile on MS VC++ or Bloodshed Dev-C++ #include <iostream> int strlen(const char *in) {
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
2
by: Tony Johansson | last post by:
Hello! I'm reading a book about C++ and there is something that I don't understand so I ask you. Below I have the text from the book and the code from the file where main is located and some...
3
by: seberino | last post by:
At top of a module I have an integer like so... foo = 4 In a function in that module I know I need to do 'global foo' to get at the value 4. .... IIRC, for dictionaries you DO NOT have...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
3
by: mrstephengross | last post by:
Hi folks. I've got a weird situation--gcc doesn't like the folllowing code snippet, but I don't know if it's correct or not. Here's the situation: In the global namespace, I've got a operator<<...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
3
by: taps128 | last post by:
I've been reading the namespace specification for the 5.3 relaese, and I can't stop thinking that they have complicated the thing unecessary. Here is what I mean. So far if you call a function...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.