2

I am currently working with an XML file that keeps race information in XML format like so

 <Row xmlns="Practice2a">
<RecordType>Qualifying Classification</RecordType> 
<_x0030_02150Position>3</_x0030_02150Position> 
<Class>250</Class> 
<_x0030_02150MachineNo>11</_x0030_02150MachineNo> 
<RiderName>Kevin James</RiderName> 
<Machine>Honda</Machine> 
<_x0030_02150ToDBehind>29.680</_x0030_02150ToDBehind> 
<_x0030_02150BestLapSpeed>97.1415157615475</_x0030_02150BestLapSpeed> 
<_x0030_02150ToDBestLapTime>5:32.274</_x0030_02150ToDBestLapTime> 
<_x0030_02150BestOnLap>7</_x0030_02150BestOnLap> 
</Row>

I want to create a plain txt file with just some of the information , I just want in kind off in a table format e.g

pos     Name      racetime    and BestLaptime

I have attempted to remove the tags from the file and create a txt file so now I get I create a line count to possibly use as delimiters for extracting the right fields.

139 Qualifying Classification
140 3
141 250
142 11
Driver Name: Machine Type: Kevin James
145 Honda
146 29.680
147 97.1415157615475
148 5:32.274

My code is getting quite out of hand and I am wondering if there is a much better way to achieve this rather than adding 14 to count each time , that's how i am displaying Driver Name:" instead of a number.

Any pointers as to how you would go about this would be a great insight.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user685590
  • 2,464
  • 5
  • 30
  • 42

3 Answers3

1

A quick solution would be to read your xml in XmlDocument (or even simpler to a dataset), and generate the text file in your c# code.

See:

Alternate approach would be to define an xslt to reformat your xml to layout of your choice. Normally its a preferred approach for generating html docs from your xml datam, though could be used to transform into normal text reports. You can read more about it on

Community
  • 1
  • 1
YetAnotherUser
  • 9,156
  • 3
  • 39
  • 53
  • I looked at all the comments and decided it was a very long winded way of achieving my goals. After completing the Authors tutorial , reading xml into a dataset , that's pretty much what I need. I can order them any way I want. – user685590 May 19 '11 at 08:53
  • I have set up a similar project in VS however I am unsure of what DataMember I need for my dataGridView I tried dataGridView1.DataMember = "Rows" . But I receive "Child list for field Rows cannot be created". XML I am currently working on looks like - - Race Classification Any Ideas What to Use?. – user685590 May 19 '11 at 09:00
1

You can parse and format it using LinqToXml:

using System.Xml.Linq

// [...]
// Load the XML, either from a string or from an url
var doc = XDocument.Parse(xmlString);

// or
var doc = XDocument.Load(new Uri(@"C:\myFile.xml"));

var result = String.Empty;

foreach (var el in doc.Descendants())
{
   // do something with it and format the data to your liking... e.g.
   result += FormatElement(el);
}

// or more compact
doc.Descendants().ToList().ForEach(el => result += FormatElement(el));

// [...]

private string FormatElement(XElement el)
{
   return String.Format("{0}: {1}", el.Name, el.Value);
}

Of course you need to adapt the FormatElement method to your needs, but this scheme should work.

LueTm
  • 2,366
  • 21
  • 31
0

XML is designed so that some features are required and may be depended-on, while other things are the choice of the author of the document. Your scheme seems to get those features exactly backwards! Which line something appears on is not guaranteed by the standard. An entire legal XML file may occupy a single line.

The whole point of XML is that the use of a standard format allows for the use of common tools. The .NET Framework has (several) XML parsing components built in to it that can read this file and give you exactly the information you are looking for. You can then output that information as text in whatever format you like.

There is no reason to parse it yourself.

And remember, if your solution includes RegEx, then you've already lost.

(I'm kidding about that last part. Sort of.)

Jeffrey L Whitledge
  • 58,241
  • 9
  • 71
  • 99