473,698 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlTextReader: Problems with accessing values and attributes

I have som XML like this:

<root>
<Course CourseCode="id1 ">
<Teacher Name="Some name"/>
<Title Titlde="Dansk Titel 1"/>
<Title Title="English Title 1"/>
<Location Place="Some place"/>
</Course>
<Course CourseCode="id2 ">
<Teacher Name="Some name"/>
<Title Title="Dansk Titel 2"/>
<Title Title="English Title 2"/>
<Location Place="Some place"/>
</Course>
</root>

I run through this data using an XmlTextReader, but I have problems
with accessing the values the correct way.

I would like the run-through to return a piece of SQL-text like

INSERT INTO Table1 (f1,f2,f3) VALUES ('id1','Dansk Titel 1','English Title 1');
INSERT INTO Table1 (f1,f2,f3) VALUES ('id2','Dansk Titel 2','English Title 2');

to later upload to a database (MySql).

I know that XmlTextReader is a kind of SAX-reader for forward-only
parsing of XML-data, but how do I tweak it to return me the
correct values?

My Existing (test)code is

string s = "";
while (xtr.Read()) {
switch(xtr.Name ) {
case "Course":
s += xtr.GetAttribut e("CourseCode ") + ",";
break;
case "Title":
s += xtr.GetAttribut e("Title") + ",";
break;
default:
break;
}
}

But it doesn't give me the correct data - and I cannot seem to merge it
correctly with the SQL-text.

Can you guys help me out here?

:o)

Thanks,

--
Jesper Stocholm - http://stocholm.dk
The Web submission form is the preferred procedure. However, if you don't
have access to the Internet you may send your submission by e-mail.
(Newsletter from 9th Conference on Reliable Software Technologies)
Nov 11 '05 #1
1 1590
Jesper Stocholm wrote:
I have som XML like this:

<root>
<Course CourseCode="id1 ">
<Teacher Name="Some name"/>
<Title Titlde="Dansk Titel 1"/>
<Title Title="English Title 1"/>
<Location Place="Some place"/>
</Course>
<Course CourseCode="id2 ">
<Teacher Name="Some name"/>
<Title Title="Dansk Titel 2"/>
<Title Title="English Title 2"/>
<Location Place="Some place"/>
</Course>
</root>

I run through this data using an XmlTextReader, but I have problems
with accessing the values the correct way.

I would like the run-through to return a piece of SQL-text like

INSERT INTO Table1 (f1,f2,f3) VALUES ('id1','Dansk Titel 1','English Title 1');
INSERT INTO Table1 (f1,f2,f3) VALUES ('id2','Dansk Titel 2','English Title 2');


Here is something you can start on:

public class Test {
static void Main(string[] args) {
XmlTextReader r = new XmlTextReader(" foo.xml");
StringBuilder sb = new StringBuilder() ;
while (r.Read()) {
if (r.NodeType == XmlNodeType.Ele ment &&
r.Name == "Course") {
sb.Length = 0;
sb.AppendFormat ("'{0}'", r["CourseCode "]);

int depth = r.Depth;
while (r.Read() && depth != r.Depth) {
if (r.NodeType == XmlNodeType.Ele ment &&
r.Name == "Title")
sb.AppendFormat (",'{0}'", r["Title"]);
}
Console.WriteLi ne("INSERT INTO Table1 (f1,f2,f3) VALUES ({0});",
sb.ToString());
}
}
}
}

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #2

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

Similar topics

0
1543
by: Svenn-Ivar Svendsen | last post by:
I'm implementing a windows application with generic support for scripting, and I'm using the microsoft script control (msscript). For vbscript/jscript I'm happy, but with python I got problems accessing COM objects that have been inserted in the script runtime by the msscript->AddObject function. Have anybody had similar problems? To be more specific the code: Monitor.Echo(logTxt, output)
1
4051
by: Gregor | last post by:
I'm having problems accessing a global array variable from within a function. I have something like this: ------------------------------------ var detail = new Array( 20 ); detail = "hello"; function ShowDetail( ) { top.frames.document.clear(); top.frames.document.write("<html><body>");
4
2026
by: Mike Clair | last post by:
Hey, I'm having an issue with CSS, JS and the DOM. It's gonna drive me batty. I am trying to access the properties of a layer in JS which have been initially set in an external CSS. The problem is that for some reason JS isn't receiving NULL for all the properties of the layer. For instance: HTML <div id="blarg">.....</div>
2
6342
by: Erwin S. Andreasen | last post by:
Hi, I have a web application where window A opens window B (same site). B later wants to do something depending on whether the window A, window.opener.document.domain, has changed. However, there doesn't seem to be a way to actually check whether A has moved to a different domain without triggering (in IE) a security violation and completely aborting the script. If the user has enabled debugging, this also brings up an annoying dialog...
1
4783
by: Casey Watson | last post by:
Hi :) I'm having some major trouble with an XML Client/Server application that I am writing. I am using NetworkStream with CryptoStream to Read and Write XML between computers. Now, whenever I write the XML, the Server section which uses the XMLTextReader Class will not accept EndElements period, and neither the ReadStartElement or ReadEndElement will work properly. However, if I only send to WriteStartElements over the network, the...
3
3756
by: Michael Iantosca | last post by:
I have a custom attribute that I attach to certain pages in my application and I want to inspect each page request as it is made to see if the custom attribute is attached to the underlying page class. If is attached I want to perform some action. How can I access custom attributes from an HttpModule? I have to pass a target to the System.Attribute.GetCustomAttribute() call to attempt to retrieve the attached attribute. I tried to access...
7
7109
by: David Laub | last post by:
I've also posted this issue to a Sun/java formum, but since it appears to be an integration issue, this may be the better place to posr: I have written a dot net/c# Web Services doesn't fully work with J2ME client a) c# web service works with c# WIndows Client (local & remote web service) b) c# web service works with J2ME client - when the Web Service is local c) c# web service fails with J2ME client - when the Web Service is remote ...
3
2040
by: Beorne | last post by:
In the classes I develop my attributes are always private and are exposed using properties. directly or to access the attributes using the properties? Does "wrapper" setter/getter properties introduce time overhead in execution? Thanks, Matteo
5
1566
by: eliben | last post by:
Hello, I have a game class, and the game has a state. Seeing that Python has no enumeration type, at first I used strings to represent states: "paused", "running", etc. But such a representation has many negatives, so I decided to look at the Enum implementation given here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413486 So, I've defined:
0
8683
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
8610
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,...
0
9031
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8902
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
8873
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...
1
6528
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
4372
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
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.