如何将XML中的特定字段解析为c#类?
我有一个如下所示的XML:如何将XML中的特定字段解析为c#类?,c#,xml,parsing,xml-parsing,xsd,C#,Xml,Parsing,Xml Parsing,Xsd,我有一个如下所示的XML: <?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/res
<?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>
我的结果应该是:
s1=Vertraqsnummer
s2=309.05
s3=已检查
我在这里查看了一些问题,但我唯一注意到的是我可以使用XsdObjectGen或XSD.exe。问题是,他们采用了整个XML,而不仅仅是我需要的部分
任何帮助都是非常感激的
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
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();
您是否需要一种允许进行许多不同映射的方法(如10+类和传入文档的不同模式),或者它只是几个类的一次任务?嘿,Alexei,它应该始终是相同的映射。意思-如果我看到第一个。。这将是我们班的第一场比赛。我应该一直得到相同的XML嘿!你能解释一下你的建议吗?我是新来的,有点迷路了。谢谢xpath可以理解为:选择值节点(undex文本节点)或(在选中了id=checked的复选标记节点下)
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();
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();