tags:

views:

122

answers:

3
+3  Q: 

C# Parsing text

I've tried searching for the answer but have not found the correct way of parsing this chunk of text:

<Hotspot X="-8892.787" Y="-121.9584" Z="82.04719" />
<Hotspot X="-8770.094" Y="-109.5561" Z="84.59527" />
<Hotspot X="-8755.385" Y="-175.0732" Z="85.12362" />
<Hotspot X="-8701.564" Y="-114.794" Z="89.48868" />
<Hotspot X="-8667.162" Y="-122.9766" Z="91.87251" />
<Hotspot X="-8802.135" Y="-111.0008" Z="82.53865" />  

I want to output each line into:

Ex. X="-8892.787" Y="-121.9584" etc...
+8  A: 

If you can possibly treat this as XML, that would be by far the better way, so, consider treating it as:

<Hotspots>
  <Hotspot X="-8892.787" Y="-121.9584" Z="82.04719" />
  <Hotspot X="-8770.094" Y="-109.5561" Z="84.59527" />
  <Hotspot X="-8755.385" Y="-175.0732" Z="85.12362" />
  <Hotspot X="-8701.564" Y="-114.794" Z="89.48868" />
  <Hotspot X="-8667.162" Y="-122.9766" Z="91.87251" />
  <Hotspot X="-8802.135" Y="-111.0008" Z="82.53865" />  
</Hotspots>

And loading it into an XmlDocument, then parsing it as follows:

var xml = "<Hotspots><Hotspot X=\"-8892.787\" Y=\"-121.9584\" Z=\"82.04719\" /></Hotspots>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

foreach (XmlNode item in doc.SelectNodes("/Hotspots/Hotspot"))
{
    Console.Write(item.Attributes["X"].Value);
    Console.Write(item.Attributes["Y"].Value);
    Console.Write(item.Attributes["Z"].Value);

    // And to get the ouput you're after:
    Console.Write("X=\"{0}\" Y=\"{1}\" Z=\"{2}\"", 
                  item.Attributes["X"].Value, 
                  item.Attributes["Y"].Value, 
                  item.Attributes["Z"].Value);
}

Note: I've used a reduced example in var xml = "..." to make it a bit more readable

Rob
XmlDocument is so retro.
Steven Sudit
@Steven, true, but it works and I've written a lot of code using it so it comes to mind first =)
Rob
@Rob: get with the program, XDocument is lighter weight, faster, and easier to use, go learn the linqiness, you'll be glad.
Jimmy Hoffa
@Jimmy, I know how to use LINQ, XDocument and do so regularly. No need for the "get with the program" attitude =) PLUS, XmlDocument is more widely available, as XDocument isn't available until 3.5/4. The OP hasn't specified which version of .net they're targeting so the lowest common denominator is a far better bet as an answer (as XmlDocument is available all the way back to 1.0).
Rob
+5  A: 

I am guessing you're not too current on your development skills, but that is not just text, it's xml and you would easily access it using Linq To XML in a fashion like so:

XDocument myXDoc = XDocument.Parse(string.Format("<?xml version=\"1.0\" encoding=\"utf-8\"?><root>{0}<root>", yourXmlString));
foreach (XElement hotspot in myXDoc.Root.Elements())
{
    Console.WriteLine(string.Format("X=\"{0}\" Y=\"{1}\"", hotspot.Attribute("X").Value, hotspot.Attribute("Y").Value));
}

I would read up on XML and Linq To XML at:

http://msdn.microsoft.com/en-us/library/aa286548.aspx

http://msdn.microsoft.com/en-us/library/bb387098.aspx

Jimmy Hoffa
Just edited<root>{0}<root> to <root>{0}</root> and its worked. Ty
Anden
A: 

Assuming each line begins with <Hotspot...

//Lines are read into (for this example) a string[] called lines
List<string> valueLines = new List<string>();

foreach(string l in lines)
{
  string x;
  string y;
  string z;

  int xStart = l.IndexOf("X");
  int yStart = l.IndexOf("Y");
  int zStart = l.IndexOf("Z");
  int closeTag = l.IndexOf(@"/");

  StringBuilder vlBuilder = new StringBuilder();

  vlBuilder.Append(l.Substring(xStart, (yStart - xStart - 1));
  vlBuilder.Append(l.Substring(yStart, (zStart - yStart - 1));
  vlBuilder.Append(l.Substring(zStart, (closeTag - zStart - 1));

  valueLines.Add(vlBuilder.ToString());
}
AllenG