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

Reading fragments / .net 2.0

I have an XML file that contains fragments, meaning there's no root
element. Node names are in my own "test" namespace. Looks like this:

<test:info date="...">Content</test:info>
<test:info date="...">Other content</test:info>
....

I have some .net 1.1 code that reads the nodes from the file one by one:

FileStream stream = new FileStream(@"file.xml", FileMode.Open);
XmlNameTable nt = new NameTable( );
XmlNamespaceManager nsMgr = new XmlNamespaceManager(nt);
nsMgr.AddNamespace("test", "http://sturmnet.org/ns/test");
XmlParserContext context = new XmlParserContext(nt, nsMgr, null,
XmlSpace.None);

XmlTextReader reader = new XmlTextReader(stream, XmlNodeType.Element,
context);

XmlDocument doc = new XmlDocument( );
while (!reader.EOF) {
XmlNode node = doc.ReadNode(reader);
// do something with the node
}

Now, using .net 2.0, docs say I'm supposed to have my XmlReader created
by XmlReader.Create instead of creating it myself. I was also hoping to
find a replacement for the reading of the nodes, because using that
XmlDocument, that's useless otherwise, just doesn't look like the most
elegant solution.

I fiddled around with this for a while, starting with

XmlReaderSettings settings = new XmlReaderSettings( );
settings.ConformanceLevel = ConformanceLevel.Fragment;
XmlReader reader = XmlReader.Create(@"file.xml", settings);
while (!reader.EOF) {
reader.Read();
// now where's my node?
}

First problem: I get an error message saying the "test" namespace is
unknown and I have no idea where I'm supposed to stick my namespace in
this model.

So, what's the PC way to get this done in .net 2.0?
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 12 '05 #1
4 1541


Oliver Sturm wrote:
I have an XML file that contains fragments, meaning there's no root
element. Node names are in my own "test" namespace. Looks like this:

<test:info date="...">Content</test:info>
<test:info date="...">Other content</test:info>
...

I have some .net 1.1 code that reads the nodes from the file one by one:

FileStream stream = new FileStream(@"file.xml", FileMode.Open);
XmlNameTable nt = new NameTable( );
XmlNamespaceManager nsMgr = new XmlNamespaceManager(nt);
nsMgr.AddNamespace("test", "http://sturmnet.org/ns/test");
XmlParserContext context = new XmlParserContext(nt, nsMgr, null,
XmlSpace.None);

XmlTextReader reader = new XmlTextReader(stream, XmlNodeType.Element,
context);

XmlDocument doc = new XmlDocument( );
while (!reader.EOF) {
XmlNode node = doc.ReadNode(reader);
// do something with the node
}

Now, using .net 2.0, docs say I'm supposed to have my XmlReader created
by XmlReader.Create instead of creating it myself. I was also hoping to
find a replacement for the reading of the nodes, because using that
XmlDocument, that's useless otherwise, just doesn't look like the most
elegant solution.


But if you want/need to have an XmlNode then you need to have an
XmlDocument as a node always belongs to its OwnerDocument.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
Martin Honnen wrote:
But if you want/need to have an XmlNode then you need to have an
XmlDocument as a node always belongs to its OwnerDocument.


Yep. Well. Any specific reason why it would have to be that way?

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 12 '05 #3
Oliver Sturm wrote:
Now, using .net 2.0, docs say I'm supposed to have my XmlReader created
by XmlReader.Create instead of creating it myself. I was also hoping to
find a replacement for the reading of the nodes, because using that
XmlDocument, that's useless otherwise, just doesn't look like the most
elegant solution.


Your .NET 1.1 code still should work. I'm sure what about
XmlReader.Create() - looks like it doesn't cover proividing namespace
bindings when reading fragments.
And what for using XmlDocument - it's not clear which problem you are
trying to solve here.

--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com
Nov 12 '05 #4
Oleg Tkachenko [MVP] wrote:
Now, using .net 2.0, docs say I'm supposed to have my XmlReader
created by XmlReader.Create instead of creating it myself. I was also
hoping to find a replacement for the reading of the nodes, because
using that XmlDocument, that's useless otherwise, just doesn't look
like the most elegant solution.

Your .NET 1.1 code still should work.


Yes, it does. I'm just trying to comply to what the docs say. Of course
they also mention that there may be cases where an XmlTextReader still
needs to be constructed directly, so maybe this is one of them.
And what for using XmlDocument - it's not clear which problem you are
trying to solve here.


You mean, with using it, or with not wanting to use it? I'm using it the
way I am because I haven't found another way to read single nodes from a
file containing a sequence of fragments.

I don't really want to use it because it looks to me like a kludge,
creating an XmlDocument I don't really need just to use a single method
(that doesn't really have anything to do with the XmlDocument, AFAICS).
As Martin said in his post on this thread, currently there seems to be a
tight binding between the XmlNode and an XmlDocument, without any
apparent reason. I was thinking that maybe with all the restructuring
work that seems to have gone into System.Xml, maybe there'd be a more
elegant way to do this now.

Never mind, it's not a real problem. Thanks!
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 12 '05 #5

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

Similar topics

0
by: Hugh Sparks | last post by:
If I configure and use two different fragment extractors on the same XML document, how can I write xslt template match patterns that distinguish which elements these fragments replaced? Details:...
4
by: Top Gun | last post by:
If I have a string that is in a constant format of, say 0154321-001, how can I parse this into two fragments: int contractid = 0154321; int contractseq = 001;
1
by: Cybertof | last post by:
Hello, What would be the best way to synchronise 2 big files ? The files have only small modifications (not more than 10Kb changed / added insided). It would a bad choice to transfer 5Mb...
7
by: Dmitry Akselrod | last post by:
Hello everyone, I am attempting to extract some header information from typical Microsoft Outlook MSG files in VB.NET. I am not after a complete message or attachments that may be enclosed. I...
9
by: Phil_Harvey | last post by:
I am redoing my website and trying to get it to do something more exciting using Javascript. I did normal Java at university and code at work in VB.NET. I have got reasonably far into what I want...
14
by: noridotjabi | last post by:
Two questions. 1)Is there any way that I can read from an executable and then execute what I have read. EXAMPLE: text text this is more text
2
by: Gustaf | last post by:
My program is constructing a document from several fragments, which are created on the fly using XSL transformations. I've managed to store the output from each transformation (each fragment) in an...
11
by: J_Zanetti | last post by:
Hello everybody, In my applications I've a ton of scripts that use remote XML file to fill forms and evaluate contents; In these scripts I always use the method SelectNode (that, with some...
4
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... We've got a lot of places in our code where we read relatively small xml user preference blocks. Currently that's creating a new XmlDocument in every spot. I was thinking we might see...
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: 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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.