C#解析文本

C#解析文本,c#,parsing,C#,Parsing,我已尝试搜索答案,但未找到解析此文本块的正确方法: <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.48

我已尝试搜索答案,但未找到解析此文本块的正确方法:

<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" />  

如果你可以把它当作XML,那将是更好的方法,所以,把它当作:

<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>

注意:我在
var xml=“…”
中使用了一个简化的示例,以使其更具可读性

我猜您的开发技能不太先进,但这不仅仅是文本,而是xml,您可以使用Linq to xml以如下方式轻松访问它:

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));
}
XDocument myXDoc=XDocument.Parse(string.Format(“{0}”,yourXmlString));
foreach(myXDoc.Root.Elements()中的XElement热点)
{
Console.WriteLine(string.Format(“X=\”{0}\“Y=”{1}\”,hospot.Attribute(“X”).Value,hospot.Attribute(“Y”).Value));
}
我将在以下位置阅读XML和Linq To XML:


假设每一行都以
开头,看起来像一个XML片段。它是带有标题的完整XML文档的一部分吗?如果不是,会不会是这样?如果是这样的话,那么就用XmlDocument和friends来解析。@Steven,没错,但它是有效的,我已经用它写了很多代码,所以我首先想到它=)@Jimmy,我知道如何使用LINQ,XDocument,并且经常这样做。不需要“使用程序获取”态度=)另外,XmlDocument的可用性更广,因为XDocument直到3.5/4才可用。OP没有指定他们针对的是.net的哪个版本,所以最低公分母是一个更好的答案(因为XmlDocument可以一直追溯到1.0)。只需将{0}编辑到{0},它就可以工作了。泰
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);
}
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));
}
//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());
}