在 C# 中用 "fields & values" 读取 XML
Reading XML with "fields & values" in C#
我们如何在 C# 中读取如下所示的 XML,以便我们拥有所有字段及其对应值的列表。
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd" xsi:schemaLocation="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd">
<businessCard>
<field type="Phone">
<value>+91-1234 53789</value>
</field>
<field type="Email">
<value>xyz@xyz.com</value>
</field>
<field type="Name">
<value>My Full Name</value>
</field>
<field type="Company">
<value>My Company Name</value>
</field>
<field type="Job">
<value>Technical Lead</value>
</field>
<field type="Text">
<value>
My Name Technical Lead Q +91-1234-567-890 © xyz@xyz.com ▲
</value>
</field>
我会把 XML 读成 Dictionary<string, string>
。键是 field type
,值是 <value>
.
的内容
XNamespace bc = "http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd";
var fields = doc
.Element(bc + "businessCard")
.Elements(bc + "field")
.ToDictionary(
x => x.Attribute("type").Value, // key
x => x.Element(bc + "value").Value); // value
我想,这可能对你更有帮助-
//this counter lets you count number of business cards
int iCounter = 1;
//Namespace associated with your businessCards
XNamespace xn = "http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd";
//Dictionary containing business cards with fields as their values.
var lists = xd.Elements(xn + "businessCard").ToDictionary(key => iCounter++, bus => bus.Elements(xn + "field").
ToDictionary(key => key.Attribute("type").Value, value => value.Element(xn + "value").Value));
我们如何在 C# 中读取如下所示的 XML,以便我们拥有所有字段及其对应值的列表。
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd" xsi:schemaLocation="http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd">
<businessCard>
<field type="Phone">
<value>+91-1234 53789</value>
</field>
<field type="Email">
<value>xyz@xyz.com</value>
</field>
<field type="Name">
<value>My Full Name</value>
</field>
<field type="Company">
<value>My Company Name</value>
</field>
<field type="Job">
<value>Technical Lead</value>
</field>
<field type="Text">
<value>
My Name Technical Lead Q +91-1234-567-890 © xyz@xyz.com ▲
</value>
</field>
我会把 XML 读成 Dictionary<string, string>
。键是 field type
,值是 <value>
.
XNamespace bc = "http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd";
var fields = doc
.Element(bc + "businessCard")
.Elements(bc + "field")
.ToDictionary(
x => x.Attribute("type").Value, // key
x => x.Element(bc + "value").Value); // value
我想,这可能对你更有帮助-
//this counter lets you count number of business cards
int iCounter = 1;
//Namespace associated with your businessCards
XNamespace xn = "http://ocrsdk.com/schema/recognizedBusinessCard-1.0.xsd";
//Dictionary containing business cards with fields as their values.
var lists = xd.Elements(xn + "businessCard").ToDictionary(key => iCounter++, bus => bus.Elements(xn + "field").
ToDictionary(key => key.Attribute("type").Value, value => value.Element(xn + "value").Value));