Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 11th, 2006, 08:03 PM
Registered User
 
Join Date: Jul 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Nick2006
Default Please help me to create XSLT condition using C#

How I can create an xml file that contains records using c#. I need to read a text file that has lots of data; however the very first row of the text file is the name of the different columns.

As an example:
DeptNo DeptName CourseNo CourseName CreditAll Semester

I need to retrieve this rows and then check condition using XSLT.

My aim is to take the above mentioned individual column and then using XSLT condition create a file so the final document will look something like this
__________________________________________________ ____________________

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:myObj="urn:myObj">

<xsl:output method="xml"/>
<xsl:template match="/">
<Universities>
  <xsl:for-each select="University">

    <DeptNumber>
      <xsl:value-of select="DeptNo"/>
    </DeptNumber>

    <DeptNM>
      <xsl:value-of select="DeptName"/>
    </DeptNM>

        <CourseNumber>
      <xsl:value-of select="CourseNo"/>
    </CourseNumber>

    <CourseNM>
      <xsl:value-of select="CourseName"/>
    </CourseNM>

    <TotalCredits>
      <xsl:value-of select="CreditAll"/>
    </TotalCredits>

        <SemesterNM>
      <xsl:value-of select="Semester"/>
        </SemesterNM>
   </xsl:for-each>
</Universities>
</xsl:template>
</xsl:transform>
__________________________________________________ ____________________

The data comes as a text file where colum names are different then the database that loads the data from this text file. So the task is to create an C# program that would check the XSLT condition and final product would be the above xml file.

I know C# program but have no clue about XSLT and XML. How I can do this?
Yours help would be greatly appreciated and thanks in advance.

Regards
Nick



 
Old July 11th, 2006, 08:21 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Location: , , Canada.
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You said: "The data comes as a text file ..."

Could you post a sample of this text file?

- Adam Kahtava [http://adam.kahtava.com]
 
Old July 11th, 2006, 09:41 PM
Registered User
 
Join Date: Jul 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Nick2006
Default

Yes and here it is and thanks

DeptNo DeptName CourseNo CourseName CreditAll Semester
111CS CS CS101 ProgrammingI 3 Fall
101Politics PS PS200 Social Politics 3 Summer
111CS CS CS221 ProgrammingII 3 Fall
131Math MATH MA801 Calculas 3 Sprng

 
Old July 11th, 2006, 11:14 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Location: , , Canada.
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

XSLT is a language for transforming XML documents into other XML documents, which means you'll have to transform your text file into an XML document before applying an XSLT.

Can you pull this data from a database or datatable? It would be easier if you could find the corresponding data in a datatable, database, or some other clearly delimited form.
Otherwise you'll have to parse the fields from this text file, and save them in XML format.

- Adam Kahtava [http://adam.kahtava.com]
 
Old July 12th, 2006, 01:23 AM
Registered User
 
Join Date: Jul 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Nick2006
Default

Adam.
   Thanks a lot however I am not sure I am understanding you. I can't pull the data from a database, wish I could.

However I can load the data from the text file whatever way its possible and then change that file into XML.

Could you please give me some example that would be great help.

Thanks again and kind regards.

Nick

 
Old July 12th, 2006, 04:56 PM
Authorized User
 
Join Date: May 2006
Location: Chico, CA, USA.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

CS instructors should really be more thorough...

The first thing to know is, "How are the fields delimited?" Can you guarantee that 1) each field is of a particular length (which is what you example appears to be), or that 2) there are tabs between the fields?

In either case, begin by reading in 1 line at a time until EoF. If 1st line, then parse out column names (keep track of index). Else, parse out data and associate with each column by index.

1) Parsing by field length is easy; use the Substring() method in a loop. See: System.String.Substring()

int index = 0;
for (int x = 0; x < num_columns; x++)
{
   field[x] = input_string.Substring(index, field_length[x]);
   index += field_length[x] + 1;
}

2) Parsing by tabs is easy; just split the string on tabs. See: System.String.Split()



Brandon
 
Old July 12th, 2006, 04:58 PM
Authorized User
 
Join Date: May 2006
Location: Chico, CA, USA.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oh, i forgot to mention in 1)

field[x] = input_string.Substring(index, field_length[x]).Trim() /// get rid of spaces while parsing.

Brandon
 
Old July 12th, 2006, 06:47 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Location: , , Canada.
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Brandon,

Your comment "CS instructors should really be more thorough" is vague. Could you give a little more detail / explanation? Are you referring to the growing concern over the state of computer science education in the Western world?

- Adam Kahtava [http://adam.kahtava.com]
 
Old July 12th, 2006, 06:48 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Location: , , Canada.
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nick,

It sounds like you have a good grasp on a solution - convert text to XML, apply XSLT to XML.

Here are a couple tutorials that may be of use:

Easy high speed reading/writing of structured binary files using C#: http://weblogs.asp.net/ralfw/archive...04/439580.aspx

Writing XML Files: Using XmlTextWriter: http://msconline.maconstate.edu/tuto.../xml09-01.aspx

XML transformation using Xslt in C#: http://www.csharpfriends.com/Article...x?articleID=63

- Adam Kahtava [http://adam.kahtava.com]




Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT and the if condition thisby XSLT 7 April 3rd, 2008 06:02 PM
Urgent:XSLT element value selection and condition amhicraig XSLT 1 December 4th, 2007 07:57 PM
create menus using xslt BrendonMelville XSLT 0 March 11th, 2006 04:52 PM
How do I create a jump menu in XSLT? kwilliams XSLT 4 January 9th, 2006 10:34 AM
How do I create a menu in xslt? TrixiePix XSLT 0 December 8th, 2005 11:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.