469,568 Members | 1,439 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

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.GetAttribute("CourseCode") + ",";
break;
case "Title":
s += xtr.GetAttribute("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 1442
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.Element &&
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.Element &&
r.Name == "Title")
sb.AppendFormat(",'{0}'", r["Title"]);
}
Console.WriteLine("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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Gregor | last post: by
4 posts views Thread by Mike Clair | last post: by
3 posts views Thread by Michael Iantosca | last post: by
5 posts views Thread by eliben | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.