Hello,
I must parse something like this: -
<html>
-
<head></head>
-
<body>
-
<using xmlns:namespace="myfile" />
-
myText
-
<p><namespace:myComponent Attribs="val"> //it crashes on 'namespace:myCOmponent'
-
</body>
-
</html>
-
But my xml parser gets error on 'namespace:myComponent'. Is there a way to overcome this? The parser is this: -
public Parser(string fileContent) {
-
_settings.ConformanceLevel = ConformanceLevel.Fragment;
-
_settings.IgnoreWhitespace = true;
-
_settings.IgnoreComments = true;
-
_textReader = XmlReader.Create(fileContent, _settings);
-
_textReader.Read();
-
XmlNodeType nType = _textReader.NodeType;
-
while (_textReader.Read()) {
-
switch (_textReader.NodeType) {
-
case XmlNodeType.Element: // The node is an element.
-
Console.Write("<" + _textReader.Name);
-
Console.WriteLine(">");
-
break;
-
case XmlNodeType.Text: //Display the text in each element.
-
Console.WriteLine(_textReader.Value);
-
break;
-
case XmlNodeType.EndElement: //Display the end of the element.
-
Console.Write("</" + _textReader.Name);
-
Console.WriteLine(">");
-
break;
-
}
-
}
-
Thanks
22 5109
Hello again,
no ideas about it?
Can you be more specific about the error you are receiving? Also, is there a reason you are declaring the namespace in such a fashion instead of as a normal xml/xhtml document: - <html xmlns:namespace="myfile">
Presumably in the example above the html document isn't what you used for real as it's incomplete.
Declaring the namespace on an element other than the root, is perfectly valid xml, so that shouldn't cause any issue.
True, it is valid, but isn't the namespace scope limited to the element that it is declared in? So using it in a self closing element means the namespace is not valid outside of the "<using..." tag?
If this is not correct then my apologies. I have limited experience with xml namespaces.
@jbm1313
That is my xml/html-like tag language. The Exception arises when the xmlReader sees the tag "<namespace:myComponent" as it says that the namespace hasn't been declared before.
I know it's not a fine XML but I need to manage that "my-own-tag language"; is there anyway to do that?
I was thinking something like this: -
while (reader.next() ) {
-
switch(reader.NodeType) {
-
case STARTELEMENT:
-
if (reader.Name == 'using' ) {
-
//add as namespace in "somewhere" the part that follow "xmlns" in 'using' tag, eg. 'namespace'
-
}
-
break;
-
case: ....................................
-
}
-
}
-
-
Ha! I missed that bit. That's entirely the problem. The namespace is declared on a self closing element, so is scoped to only that element. When the xmlreader encounters the namespace further down it throws a "namespace undeclared" exception. What you've got otherwise is invalid XML, which no xmlparser can read.
Change your xml to below or move the namespace declaration to the myComponent element. - <?xml version="1.0" encoding="utf-8" ?>
-
<html>
-
<head></head>
-
<body>
-
<using xmlns:namespace="myfile">
-
myText
-
<p>
-
<namespace:myComponent Attribs="val"> //it crashes on 'namespace:myCOmponent'</namespace:myComponent>
-
</p>
-
</using>
-
</body>
-
-
</html>
or this - <?xml version="1.0" encoding="utf-8" ?>
-
<html>
-
<head></head>
-
<body>
-
<using/>
-
myText
-
<p>
-
<namespace:myComponent xmlns:namespace="myfile" Attribs="val"> //it crashes on 'namespace:myCOmponent'</namespace:myComponent>
-
</p>
-
</body>
-
-
</html>
do you assure me that there is no way to keep my namespace way, and manage it in someway at programming language level?
thanks
I don't believe that you can without resorting to writing your own custom XML classes from scratch. Even if you add the schema to your xml document object programmatically (which can be done), the namespace prefix will still throw an exception during parsing because it does not have valid scope in your example case.
What you are talking about doing here is diverging from the way that the XML standard handles namespaces. That is fundamentally breaking to the .Net XML objects.
If I were you, I would take one of the changes suggested by simongh.
understood. Can you tell me what do you intend with 'schema'? I mean, if I use the tag </using>, the XMLReader work perfectly; so what do I have to add (with schema I mean)
regards.
@mickey0
Sorry, my mistake. I meant to say namespace, not schema.
basically doesn't change my question...........
Thanks for asking that last question. It made me dig deaper into the .Net XML objects.
In order to get your original scenario to work you would have to read the XML as a string, find the "<using..." tags, and then add them to a separate namespace manager that you use for parsing the string. MSDN has an article that shows something similar to what you are talking about. MSDN Article
This will result in an XmlReader that reads through your xml file without generating an error.
Be aware that if you did something like this: - XmlDocument myDoc = new XmlDocument();
-
myDoc.Load(reader);
then myDoc.OuterXml will reflect the addition of the new namespaces and you will wind up with a document very similar to version 2 of simongh's suggested fixes.
Using the XmlNamespaceManager does provide a work-around, but I still think that simongh's suggestions would be more correct because you would start off with readable, portable, and correct XML.
yes, it should be great; but I repeat this for completeness; are you sure that I can exploit what you say with out know the namespace keyword in advance? Can I embedded what you say here? -
while (reader.next() ) {
-
switch(reader.NodeType) {
-
case STARTELEMENT:
-
if (reader.Name == 'using' ) {
-
//add as namespace in "somewhere" the part that follow "xmlns" in 'using' tag, eg. 'namespace'
-
ONLY HERE I KNOW THE NAME OF THE NAMESPACE i WILL ENCOUTER LATER, EG. 'NAMESPACE'....
-
}
-
break;
-
case: ....................................
-
}
-
}
-
-
As far as I can tell, no your example code would NOT work. You cannot add new namespaces once the reader object has been instantiated. When I tried that in my example code I received an exception.
You would have to pull out the "<using..." tags manually BEFORE you instantiated your reader object. Changes to the XmlNamespaceManager object do not affect the XmlReader after the XmlReader has been instantiated.
@jbm1313
thanks. not to insist but maybe there is a way to do that? (you've understood fine what I was speaking of)
Can you tell me how switch off the namespace checking?
Pulling out the "<using..." tag is not necessarily difficult, just troublesome and probably slow. You would essentially have to do something like this: -
NameTable nt = new NameTable();
-
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
-
-
int usingStartIndex = 0;
-
int usingPrefixIndex = 0;
-
string usingPrefix = null;
-
-
int usingURIIndex = 0;
-
string usingURI = null;
-
usingStartIndex = xmlFrag.IndexOf("<using", usingStartIndex);
-
-
while (usingStartIndex >= 0)
-
{
-
usingPrefixIndex = xmlFrag.IndexOf(':', usingStartIndex) + 1;
-
usingPrefix = xmlFrag.Substring(usingPrefixIndex, xmlFrag.IndexOf(' ', usingPrefixIndex) - usingPrefixIndex);
-
-
usingURIIndex = xmlFrag.IndexOf("=\"", usingStartIndex) + 2;
-
usingURI = xmlFrag.Substring(usingURIIndex, xmlFrag.IndexOf('\"', usingStartIndex));
-
-
//add namespace info to namespace manager
-
nsmgr.AddNamespace(usingPrefix, usingURI);
-
-
usingStartIndex = xmlFrag.IndexOf("<using", usingStartIndex);
-
}
-
This is untested, but it does compile.
To turn off the namespace checking, use: - reader.Namespaces = false;
This line MUST occur before any call to the Read() method on your reader object.
What version of the .Net framework are you using?
using .NET 2005; BTW I've installed .net Frameworks 2.0 SP2, 3.0 SP2, 3.5 SP1 (why so many?)
I saw them but I didn't find anything similar to 'disable namespace'.....
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Phong Ho |
last post by:
Hi everyone,
I try to write a simple web crawler. It has to do the following:
1) Open an URL and retrieve a HTML file.
2) Extract news headlines from the HTML file
3) Put the headlines into a...
|
by: Alex Hopson |
last post by:
I'm trying to read an html file from my local server into a string, I'm
using the following code:
$attfile = $attachment; //create filenames
$file_name = basename ($attfile);
$lines =...
|
by: alejandro lapeyre |
last post by:
How can I load / parse an HTML file with .NET?
Thanks!
Best regards,
Alejandro Lapeyre
|
by: Pranav Shah |
last post by:
What is the differrence between using the "using" caluse outside of the
namespace definition and inside the namespace.
Example Outside:
using System;
namespace Example.Outside
{
}
|
by: terrorix |
last post by:
I also posted this article at: microsoft.public.dotnet.framework.aspnet
--------
Hi,
I Have this sql select(MS SQL):
select a.ID,
b.ID
from Table as a
|
by: terrorix |
last post by:
Hi,
I Have this sql select(MS SQL):
select a.ID,
b.ID
from Table as a
left outer
join table AS b
|
by: Keith Patrick |
last post by:
Can someone tell me the difference in terms of actual implications using:
namespace MyNamespace {
using System;
class MyClass {...}
}
vs.
using System;
|
by: suresh_nsnguys |
last post by:
Hi,
I will allow users to download one zip file(contain HTML file and 1 image directory with -gif,jpeg files) to his local hard disk.
HTML file contain source code to display images
...
|
by: Chris Fairles |
last post by:
Possible? Take the following example:
===================
//test1.hpp
namespace B {
typedef int type;
void foo() {}
}
===================
//test.hpp
|
by: dbsmilr |
last post by:
This is what I want to do:
<html>
<xml id="myXml">
<book title="book1" />
<book title="book2" />
</xml>
<div id="out"><!-- I would output the xml formatted nicely for the
user in this...
|
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...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |