如何在 WebAPI .NET 中使用 XML 作为数据源
How can I use XML as a data source in WebAPI .NET
我的 WebAPI 有一些问题。我遵循了 Microsoft 的指南并且有效:https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api?fbclid=IwAR0tEMDMX3URn2YO0pwnTTAbY2RuGkdu-HUobznac4Lwus6rOVPSeiX-lFs
现在我希望能够从我的 XML 文件中获取数据,而不是指南使用的硬编码值。我已经尝试搜索这个但不确定我找到了正确的东西。我该怎么做?
我试过的代码:
public IEnumerable<Book> GetAllProducts()
{
XDocument doc = XDocument.Load("C:\Users\Name\Desktop\products.xml");
foreach (XElement element in doc.Descendants("Catalog")
.Descendants("Product"))
{
Product product = new Product();
product.Id = element.Element("Id").Value;
product.Name = element.Element("Name").Value;
product.Category = element.Element("Category").Value;
product.Added_Date = element.Element("Added_Date").Value;//this will not work because its not a string
product.Price = element.Element("Price").Value;//this will not work because its not a string
products.Add(product);
}
return products;
}
XML代码:
<?xml version="1.0"?>
<catalog>
<product id="P1">
<name>Royal Gala</name>
<category>Apple</category>
<country>New Zeeland</country>
<price>33.00</price>
<added_date>2011-01-11</added_date>
<description>This is a lovely red apple.</description>
</product>
<product id="P2">
<name>Granny Smith</name>
<category>Apple</category>
<country>Australia</country>
<price>33.00</price>
<added_date>2013-12-25</added_date>
<description>This is a lovely green apple.</description>
</product>
</catalog>
你只需要将string
解析为特定类型
Product product = new Product();
//..
product.Added_Date = DateTime.Parse(element.Element("Added_Date").Value);
product.Price = double.Parse(element.Element("Price").Value); //or float, or decimal
并考虑使用 Elements
方法而不是 Descendants
因为最后一个 returns 所有 children 及其内部 children 等等 Elements
returns一级children
var productNodes = doc.Root.Elements("product"); //note this is case sensitive
foreach (XElement element in productNodes) {
Product product = new Product();
//..
}
我的 WebAPI 有一些问题。我遵循了 Microsoft 的指南并且有效:https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api?fbclid=IwAR0tEMDMX3URn2YO0pwnTTAbY2RuGkdu-HUobznac4Lwus6rOVPSeiX-lFs
现在我希望能够从我的 XML 文件中获取数据,而不是指南使用的硬编码值。我已经尝试搜索这个但不确定我找到了正确的东西。我该怎么做?
我试过的代码:
public IEnumerable<Book> GetAllProducts()
{
XDocument doc = XDocument.Load("C:\Users\Name\Desktop\products.xml");
foreach (XElement element in doc.Descendants("Catalog")
.Descendants("Product"))
{
Product product = new Product();
product.Id = element.Element("Id").Value;
product.Name = element.Element("Name").Value;
product.Category = element.Element("Category").Value;
product.Added_Date = element.Element("Added_Date").Value;//this will not work because its not a string
product.Price = element.Element("Price").Value;//this will not work because its not a string
products.Add(product);
}
return products;
}
XML代码:
<?xml version="1.0"?>
<catalog>
<product id="P1">
<name>Royal Gala</name>
<category>Apple</category>
<country>New Zeeland</country>
<price>33.00</price>
<added_date>2011-01-11</added_date>
<description>This is a lovely red apple.</description>
</product>
<product id="P2">
<name>Granny Smith</name>
<category>Apple</category>
<country>Australia</country>
<price>33.00</price>
<added_date>2013-12-25</added_date>
<description>This is a lovely green apple.</description>
</product>
</catalog>
你只需要将string
解析为特定类型
Product product = new Product();
//..
product.Added_Date = DateTime.Parse(element.Element("Added_Date").Value);
product.Price = double.Parse(element.Element("Price").Value); //or float, or decimal
并考虑使用 Elements
方法而不是 Descendants
因为最后一个 returns 所有 children 及其内部 children 等等 Elements
returns一级children
var productNodes = doc.Root.Elements("product"); //note this is case sensitive
foreach (XElement element in productNodes) {
Product product = new Product();
//..
}