XML 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfCompany xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Company>
<CompanyID>Company A</CompanyID>
<CompanyName>Company A</CompanyName>
<CompanyRecID>1491</CompanyRecID>
<Contacts>
<Contact>
<City>Birmingham</City>
<Country>United States</Country>
<FirstName>Steven</FirstName>
</Contact>
<Contact>
<City>Birmingham</City>
<Country>United States</Country>
<FirstName>Natalie</FirstName>
</Contact>
</Contacts>
</Company>
<Company>
<CompanyID>Company B</CompanyID>
<CompanyName>Company B</CompanyName>
<CompanyRecID>1492</CompanyRecID>
<Contacts>
<Contact>
<City>Birmingham</City>
<Country/>
<FirstName>Greg</FirstName>
</Contact>
<Contact>
<City>Birmingham</City>
<Country/>
<FirstName>Robert</FirstName>
</Contact>
</Contacts>
</Company>
</ArrayOfCompany>
使用两行代码,我可以将该 XML 文件提取到数据集中:
var dsCustomer = new System.Data.DataSet("CustomerDataSet");
dsCustomer.ReadXml(System.IO.Path.Combine(currentAssemblyDirectoryName, "company.xml"));
.NET 理解架构。这是我使用 ds.WriteXmlSchema 命令时的样子:
给定公司名称,如何使用 LINQ 查询返回联系人?