473,799 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="...">Cont ent</test:info>
<test:info date="...">Othe r content</test:info>
....

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

FileStream stream = new FileStream(@"fi le.xml", FileMode.Open);
XmlNameTable nt = new NameTable( );
XmlNamespaceMan ager nsMgr = new XmlNamespaceMan ager(nt);
nsMgr.AddNamesp ace("test", "http://sturmnet.org/ns/test");
XmlParserContex t context = new XmlParserContex t(nt, nsMgr, null,
XmlSpace.None);

XmlTextReader reader = new XmlTextReader(s tream, XmlNodeType.Ele ment,
context);

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

Now, using .net 2.0, docs say I'm supposed to have my XmlReader created
by XmlReader.Creat e 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

XmlReaderSettin gs settings = new XmlReaderSettin gs( );
settings.Confor manceLevel = ConformanceLeve l.Fragment;
XmlReader reader = XmlReader.Creat e(@"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 1571


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="...">Cont ent</test:info>
<test:info date="...">Othe r content</test:info>
...

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

FileStream stream = new FileStream(@"fi le.xml", FileMode.Open);
XmlNameTable nt = new NameTable( );
XmlNamespaceMan ager nsMgr = new XmlNamespaceMan ager(nt);
nsMgr.AddNamesp ace("test", "http://sturmnet.org/ns/test");
XmlParserContex t context = new XmlParserContex t(nt, nsMgr, null,
XmlSpace.None);

XmlTextReader reader = new XmlTextReader(s tream, XmlNodeType.Ele ment,
context);

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

Now, using .net 2.0, docs say I'm supposed to have my XmlReader created
by XmlReader.Creat e 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.Creat e 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.Creat e() - 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.Creat e 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
1472
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: My xml document contains, among other elements, two distinct elements that need external java processing by a custom-made Cocoon serializer that converts the contents of these elements to "other stuff".
4
5181
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
1919
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 regarding only 10Kb of changes. If only a few bytes are added/modified it's does not worth it to transfer the whole file.
7
5047
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 am particularly interested in the Message ID field. I have examined MSG files in notepad and hex editors. I can see that the Internet Headers are there and present. I can do a search for Message-ID and locate it without any problems in notepad....
9
2158
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 to do but am having some issues. Firstly I am using an XmlHttpRequest to pull html fragments out of files and wish to put them into the main doccument inplace of a <span></span> which has a given id. The user enters a "command" into a TextField...
14
1677
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
1562
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 XmlDocument object, and I can include that in the collected document using code like this: w.WriteRaw(xmlDoc.DocumentElement.InnerXml) The problem is that when I use WriteRaw(), there's no awareness of which namespaces are already in scope...
11
6110
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 workaround, works fine also in Mozilla). I've just found out that this method doesnt work in Safari browser, therefore my applications are not usable by this browser. Can anyone provide me any solution or workaround to be able to read XML files in...
4
3276
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 some speed improvements by having one, central XmlDocument and using doc.ReadNode() to process all of the fragments. Other than pumping up the NameTable with a mish-mash of different node names and namespaces, are there any other implications I'm...
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.