By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,101 Members | 1,426 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,101 IT Pros & Developers. It's quick & easy.

roundtripping and xmlns attributes

P: n/a
I am endeavouring to use .NET v2.0 XML facilities to "roundtrip" reading and
writing XML documents - i.e. to end up with _exactly_ what I started with,
but I can't seem to figure out how to get an XMLReader to report "xmlns"
attributes. It seems to report an

"xmlns='MyDefaultURI'"

with incorrect data, and doesn't appear to report

"xmlns:myprefix="MyPrefixURI"

prefix declarations at all. Yes, I know that it correctly processes and
resolves namespaces and prefixes etc, but I want to be able to accurately
report the attribute occurences themselves. Is this possible?

This functionality is easily achieved using Expat btw.

thanks in advance,

gary
Sep 23 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Whoops, I probably should have mentioned that the "report"ing I was referring
to was the result of calls to "MoveToNextAttribute" on the XMLReader object.

cheers,

gary

"Gary Stephenson" wrote:
I am endeavouring to use .NET v2.0 XML facilities to "roundtrip" reading and
writing XML documents - i.e. to end up with _exactly_ what I started with,
but I can't seem to figure out how to get an XMLReader to report "xmlns"
attributes. It seems to report an

"xmlns='MyDefaultURI'"

with incorrect data, and doesn't appear to report

"xmlns:myprefix="MyPrefixURI"

prefix declarations at all. Yes, I know that it correctly processes and
resolves namespaces and prefixes etc, but I want to be able to accurately
report the attribute occurences themselves. Is this possible?

This functionality is easily achieved using Expat btw.

thanks in advance,

gary
Sep 23 '06 #2

P: n/a


Gary Stephenson wrote:
Whoops, I probably should have mentioned that the "report"ing I was referring
to was the result of calls to "MoveToNextAttribute" on the XMLReader object.

Sorry, I can't reproduce that problem, e.g. file.xml is

<?xml version="1.0" encoding="UTF-8"?>
<gods xmlns="http://example.com/2006/gods"
xmlns:xhtml="http://www.w3.org/1999/xhtml"/>

and a simple C# program snippet is

using (XmlReader xmlReader = XmlReader.Create(@"file.xml")) {
while (xmlReader.Read()) {
if (xmlReader.NodeType == XmlNodeType.Element) {
Console.WriteLine(
"Found element with local name: {0}, namespace URI: {1}.",
xmlReader.LocalName, xmlReader.NamespaceURI);
while (xmlReader.MoveToNextAttribute()) {
Console.WriteLine(
"Found attribute with prefix: {0}, local name: {1}, namespace URI: {2}",
xmlReader.Prefix, xmlReader.LocalName, xmlReader.NamespaceURI);
}
Console.WriteLine();
}
}
}

output is

Found element with local name: gods, namespace URI:
http://example.com/2006/gods.
Found attribute with prefix: , local name: xmlns, namespace URI:
http://www.w3.org/2000/xmlns/
Found attribute with prefix: xmlns, local name: xhtml, namespace URI:
http://www.w3.org/2000/xmlns/

so both the default namespace declaration and the other namespace
declaration are reported as attributes. Such attributes are by
definition in the namespace with URI http://www.w3.org/2000/xmlns/, so
that is also correct.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 23 '06 #3

P: n/a
Thanks Martin,

You are correct, of course. I should definitely have tried to post an
example demonstrating my "problem". Presumably this would have prevented my
initial post
as the problem was, of course, a dumb-assed coding error

Apologies to all for wasting valuable bandwidth,

gary

"Martin Honnen" wrote:
>

Gary Stephenson wrote:
Whoops, I probably should have mentioned that the "report"ing I was referring
to was the result of calls to "MoveToNextAttribute" on the XMLReader object.


Sorry, I can't reproduce that problem, e.g. file.xml is

<?xml version="1.0" encoding="UTF-8"?>
<gods xmlns="http://example.com/2006/gods"
xmlns:xhtml="http://www.w3.org/1999/xhtml"/>

and a simple C# program snippet is

using (XmlReader xmlReader = XmlReader.Create(@"file.xml")) {
while (xmlReader.Read()) {
if (xmlReader.NodeType == XmlNodeType.Element) {
Console.WriteLine(
"Found element with local name: {0}, namespace URI: {1}.",
xmlReader.LocalName, xmlReader.NamespaceURI);
while (xmlReader.MoveToNextAttribute()) {
Console.WriteLine(
"Found attribute with prefix: {0}, local name: {1}, namespace URI: {2}",
xmlReader.Prefix, xmlReader.LocalName, xmlReader.NamespaceURI);
}
Console.WriteLine();
}
}
}

output is

Found element with local name: gods, namespace URI:
http://example.com/2006/gods.
Found attribute with prefix: , local name: xmlns, namespace URI:
http://www.w3.org/2000/xmlns/
Found attribute with prefix: xmlns, local name: xhtml, namespace URI:
http://www.w3.org/2000/xmlns/

so both the default namespace declaration and the other namespace
declaration are reported as attributes. Such attributes are by
definition in the namespace with URI http://www.w3.org/2000/xmlns/, so
that is also correct.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.