2014-03-05 38 views
1

对于在单一个项目,我需要解析XML结果文件,该文件是这样的:XML文件到C#对象

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ocrsdk.com/schema/resultDescription-1.0.xsd http://ocrsdk.com/schema/resultDescription-1.0.xsd" xmlns="http://ocrsdk.com/schema/resultDescription-1.0.xsd"> 
    <page index="0"> 
    <text id="print" left="160" top="349" right="339" bottom="384"> 
     <value>Vertraqsnummer:</value> 
     <line left="167" top="366" right="326" bottom="384"> 
     <char left="167" top="366" right="180" bottom="382">V</char> 
     <char left="182" top="370" right="192" bottom="382">e</char> 
     <char left="194" top="370" right="199" bottom="382">r</char> 
     <char left="199" top="367" right="205" bottom="382">t</char> 
     <char left="206" top="370" right="212" bottom="382">r</char> 
     <char left="213" top="370" right="223" bottom="382">a</char> 
     <char left="224" top="370" right="234" bottom="384">q</char> 
     <char left="236" top="371" right="245" bottom="383">s</char> 
     <char left="247" top="371" right="256" bottom="382">n</char> 
     <char left="258" top="371" right="268" bottom="383">u</char> 
     <char left="270" top="370" right="285" bottom="383">m</char> 
     <char left="287" top="370" right="302" bottom="382"> 
      <charRecVariants> 
      <variant charConfidence="22">m</variant> 
      <variant charConfidence="-1">rn</variant> 
      </charRecVariants>m</char> 
     <char left="304" top="370" right="314" bottom="382">e</char> 
     <char left="316" top="370" right="322" bottom="382">r</char> 
     <char left="324" top="370" right="326" bottom="382" suspicious="true">:</char> 
     </line> 
    </text> 
    <text id="handprint" left="387" top="1035" right="635" bottom="1089"> 
     <value>309.05</value> 
     <line left="398" top="1045" right="633" bottom="1089"> 
     <char left="398" top="1052" right="426" bottom="1088">3</char> 
     <char left="423" top="1061" right="455" bottom="1089" suspicious="true">0</char> 
     <char left="482" top="1055" right="505" bottom="1089">9</char> 
     <char left="507" top="1084" right="512" bottom="1087">.</char> 
     <char left="520" top="1058" right="549" bottom="1089">0</char> 
     <char left="546" top="1045" right="633" bottom="1089" suspicious="true">5</char> 
     </line> 
    </text> 
    <checkmark id="checked" left="883" top="427" right="928" bottom="469"> 
     <value>checked</value> 
    </checkmark> 
    <checkmark id="not checked" left="884" top="511" right="928" bottom="554"> 
     <value>unchecked</value> 
    </checkmark> 
    <barcode id="leftBarcode" left="46" top="1048" right="128" bottom="1350"> 
     <value encoding="Base64">QkYxMDExNQ==</value> 
    </barcode> 
    </page> 
</document> 

我只需要的元素,从它,我需要创建许多新对象元素的数量。像这样:

class result{ 
string first; 
string second; 
} 

结果应该只包含value元素的值。

我什么都试过,但我似乎无法理解怎么办呢...... (说实话这是第一次我处理XML文件...)

如何有任何建议解析XML文件?

+1

可能的重复http://stackoverflow.com/questions/55828/how-does-one-parse-xml-files – ovaltein

+1

重复http://stackoverflow.com/questions/10518372/how-to-deserialize-xml-对象 –

+0

结果是第一和第二个什么?你的xml中没有任何名字第一或第二。 –

回答

1

为您创建一个具有您所需属性的C#类。请注意,您将需要单独的类来表示嵌套的元素和属性。然后你使用XMLSerializer来反序列化你的XML,类似这样:

public static YourClass FromXmlString(string xmlString) 
{ 
    var reader = new StringReader(xmlString); 
    var serializer = new XmlSerializer(typeof(YourClass)); 
    return (YourClass)serializer.Deserialize(reader); 
} 

你说你只想要一些字段;这是一段时间,因为我已经这样做了,但我相信你C#类中缺少的所有属性都将被忽略。此外,您还可以映射你的XML元素使用的XMLElement属性名称不同的C#属性:

[XmlElement("some-element-name")] 
public string MyProperty { get; set; } 

的属性,你也可以使用XMLAttribute属性映射:

[Serializable] 
[XMLElement("page"] 
public sealed class Page 
{ 
    [XmlAttribute("index")] 
    public int Index { get; set; } 
} 

在这里看到的例子: XML string deserialization into c# object

0

让我们以最好的方式开始,一次一件。我会告诉你如何创建你的变体类。

public class XVariant 
{ 
    public readonly XElement self; 
    public XVariant(XElement element = null) 
    { 
     self = element ?? new XElement("variant"); 
    } 

    public int CharConfidence 
    { 
     get { return (int)self.Attribute("charConfidence"); } 
     set 
     { 
      XAttribute cc = self.Attribute("charConfidence"); 
      if (cc == null) 
       self.Add(cc = new XAttribute("charConfidence")); 
      cc.Value = value.ToString(); 
     } 
    } 

    public string Value 
    { 
     get { return self.Value; } 
     set { self.Value = value; } 
    } 
} 

现在我们可以读取所有variant元素搭配:

XElement root = XElement.Load(file); // or .Parse(string) 
List<XVariant> variants = root.Descentants("variant") 
           .Select(x => new XVariant(x)) 
           .ToList(); 
foreach(XVariant variant in variants) 
{ 
    Console.WriteLine(variant.CharConfidence.ToString() + " " + variant.Value); 
} 

正如你可以看到,这是比较简单的,在一个时间来写一个类(元),当你打破它像这样。根据您的目的,我写了它,以便您可以添加/更改值。