我完全不熟悉从文件中读取数据,特别是 xml 文件。我的文件bin\debug
夹中有一个 xml 文件,该 xml 文件如下所示:
<Companies>
<Company>
<Name>Name1</Name><Code>11014</Code>
<MaintenancePercentage>15.9</MaintenancePercentage>
<Sales>
<Sale>
<Code>19538</Code>
<Title>ABC</Title>
<Date>2009-04-29T00:00:00</Date>
<Category>Category1</Category>
<Amount>6543.39</Amount>
</Sale>
<Sale>
<Code>19539</Code>
<Title>xyz</Title>
<Date>2009-04-30T00:00:00</Date>
<Category>Category2</Category>
<Amount>654.39</Amount>
</Sale>
</Sales>
</Company>
<Company>
<Name>Name1</Name><Code>11014</Code>
<MaintenancePercentage>15.9</MaintenancePercentage>
<Sales>
<Sale>
<Code>19538</Code>
<Title>ABC</Title>
<Date>2009-04-29T00:00:00</Date>
<Category>Category1</Category>
<Amount>6543.39</Amount>
</Sale>
<Sale>
<Code>19539</Code>
<Title>xyz</Title>
<Date>2009-04-30T00:00:00</Date>
<Category>Category2</Category>
<Amount>654.39</Amount>
</Sale>
</Sales>
</Company>
</Companies>
这是我的课:
public class Company
{
public string Name;
public string Code;
public double MaintenancePercentage;
public double AverageSales;
public double TotalSales;
public double TotalMaintenanceFee;
public List<Sales> Saleses;
}
public class Sales
{
public string Code;
public string Title;
public DateTime DateTime;
public string Category;
public double Amount;
}
现在我想从 xml 文件中读取数据并存储它们。
如何从 xml 文件中读取值?
例如
var company = new Company();
company.Name = //name from xml file
//and so on
foreach(sales in company)
{
sales.code = //code from xml file
sales.title = //title from xml file
//and so on
}
这是我到目前为止使用谷歌的搜索结果所做的,但我不知道现在该做什么
var doc = new XmlDocument();
doc.Load(@"export.xml");
var root = doc.DocumentElement;
if (root == null)
{
return;
}
var company = root.SelectNodes("Company");
if (company == null)
{
return;
}
else
{
foreach (var companyData in company)
{
var title =
}
}
最后一段代码至少有意义吗?