Click to See Complete Forum and Search --> : transform xml tag to css tag in c#


kolesnik
August 28th, 2005, 01:13 PM
Hi there,
the question is related to C# and XML.

I have simple xml file
<pre:book>
<pre:author> Mark Twain </pre:author>
<pre:title>Tom Sawyer</pre:title>
<pre:chapter>text stuff
<pre:highlight>important stuff</pre:highlight>
more ordinary text
</pre:chapter>
</pre:book>

I have to parse this file and store its elements into database.
In C# I used XmlTextReader because of it's processing speed. There is a part of my code:

XmlTextReader reader = new XmlTextReader(xmlPath);
while (reader.Read()){
if(reader.NodeType == XmlNodeType.Element){
//Get the name of the XML token
switch (reader.Name.ToLower()){
case "pre:author":
saveToDatabase("author",reader.ReadString());
break;
case "pre:title":
saveToDatabase("title",reader.ReadString());
break;
....


In the database I have a column named 'chapter' to store the text from element <chapter>. But I can not use reader.ReadString() method to read text in the element chapter, bacause it reads just to the first <highlight> element. I need method that will change tags <highlight> to <span class="highlight"> inside element <chapter>. The purpose of this trasformation is the ability to get easily "chapter" information from the database and put it on the web using css styles.
I suppose I need to use XSLT transformation (I'm new to xml/xslt) Is there a way to do such a transformation for one node
(<chapter>) on the fly. Or do I need to do the transformation of the whole xml file and after this step to do parsing?

I appreciate any help.

mehdi62b
August 29th, 2005, 01:43 PM
The purpose of this trasformation is the ability to get easily "chapter" information from the database and put it on the web using css styles.
I suppose I need to use XSLT transformationwell,it's better to have a suitable XSL file which generate the HTML tags accordingly then you can use XslTransform class which you could give it your XML and XSL file and get the result HTML file.
by the way if you need to itterate through nested tags you would need a XmlDocument then you can get nodes through its GetElementByTagname and check their ChildNodes property.