首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将XML中的特定字段解析为c#类?

如何将XML中的特定字段解析为c#类?
EN

Stack Overflow用户
提问于 2014-03-07 17:40:11
回答 1查看 142关注 0票数 0

我的XML如下所示:

代码语言:javascript
复制
<?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="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="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>

我只想解析写XXX的字段,把值放在我的c#类的字段下。

例如,对于这个XML,我想取以下值:

代码语言:javascript
复制
 **<value>Vertraqsnummer:</value>**
**<value>309.05</value>**
 **<value>checked</value>**

例如,使用类"A“:

代码语言:javascript
复制
class A
{
   public string s1;
   public string s2;
   public string s3;
}

我的结果应该是: s1 = Vertraqsnummer s2 = 309.05 s3 = checked

我在这里看了一些问题,但我唯一注意到的是我可以使用XsdObjectGen或XSD.exe。问题是,它们获取了整个XML,而不仅仅是我需要的部分。

任何帮助都是徒劳无功的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-07 18:01:08

代码语言:javascript
复制
XmlNamespaceManager nsMgr = new XmlNamespaceManager(new NameTable());
nsMgr.AddNamespace("ns", "http://ocrsdk.com/schema/resultDescription-1.0.xsd");

var result = XDocument.Load(filename)
    .XPathSelectElements("//ns:text/ns:value|//ns:checkmark[@id='checked']/ns:value", nsMgr)
    .Select(x => x.Value)
    .ToList();

或者没有XPATH

代码语言:javascript
复制
XNamespace ns = "http://ocrsdk.com/schema/resultDescription-1.0.xsd";
var xDoc = XDocument.Load(filename);

var result = xDoc.Descendants(ns + "text")
            .Union(xDoc.Descendants(ns + "checkmark").Where(c => (string)c.Attribute("id") == "checked"))
            .Select(x => x.Element(ns + "value").Value)
            .ToList();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22256986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档