从xml加载数据.后代返回空值
本文关键字:返回 空值 后代 数据 xml 加载 | 更新日期: 2023-09-27 18:15:08
我使用xml序列化器将数据集内容导出为xml。在一个单独的项目中(PCL项目)。我试图读取xml文件到一个对象类使用Xdocuments后裔,但它没有返回任何值。
我错过了xml文档中的XNamespace的东西吗?
Stream stream = assembly.GetManifestResourceStream("FatForWeightLossApp.Resources.IngredientExport.xml");
XDocument xml = XDocument.Load(stream);
Ingredient model = new Ingredient();
XNamespace ns = "http://tempuri.org/FatForWeightLossDataSet.xsd";
var ingredient = from r in xml.Descendants(ns + "IngredientTableDataTable")
select new
{
Id = r.Element("Id").Value,
Name = r.Element("Name").Value,
Calories = r.Element("Calories").Value,
Carbs = r.Element("Carbs").Value,
Fat = r.Element("Fat").Value,
Protein = r.Element("Protein").Value,
Cholesterol = r.Element("Cholesterol").Value,
Sodium = r.Element("Sodium").Value,
Sugars = r.Element("Sugars").Value,
Fibre = r.Element("Fibre").Value,
Type = r.Element("Grams").Value
};
foreach (var i in ingredient)
{
model.Id = Convert.ToInt32(i.Id);
model.Name = i.Name;
model.Calories = Convert.ToInt32(i.Calories);
model.Carbs = Convert.ToInt32(i.Carbs);
model.Fat = Convert.ToInt32(i.Fat);
model.Protein = Convert.ToInt32(i.Protein);
model.Cholesterol = Convert.ToInt32(i.Cholesterol);
model.Sodium = Convert.ToInt32(i.Sodium);
model.Sugars = Convert.ToInt32(i.Sugars);
model.Fibre = Convert.ToInt32(i.Fibre);
model.Type = i.Type;
database.Insert(model);
}
<?xml version="1.0" encoding="utf-8"?>
<IngredientTableDataTable>
<xs:schema id="FatForWeightLossDataSet" targetNamespace="http://tempuri.org/FatForWeightLossDataSet.xsd" xmlns:mstns="http://tempuri.org/FatForWeightLossDataSet.xsd" xmlns="http://tempuri.org/FatForWeightLossDataSet.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="FatForWeightLossDataSet" msdata:IsDataSet="true" msdata:MainDataTable="http_x003A__x002F__x002F_tempuri.org_x002F_FatForWeightLossDataSet.xsd_x003A_IngredientTable" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="IngredientTable">
<xs:complexType>
<xs:sequence>
<xs:element name="Id" type="xs:int" />
<xs:element name="Name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="50" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Calories" type="xs:decimal" />
<xs:element name="Carbs" type="xs:decimal" />
<xs:element name="Fat" type="xs:decimal" />
<xs:element name="Protein" type="xs:decimal" />
<xs:element name="Cholesterol" type="xs:decimal" />
<xs:element name="Sodium" type="xs:decimal" />
<xs:element name="Sugars" type="xs:decimal" />
<xs:element name="Fibre" type="xs:decimal" minOccurs="0" />
<xs:element name="Tbsp" type="xs:decimal" minOccurs="0" />
<xs:element name="Cups" type="xs:decimal" minOccurs="0" />
<xs:element name="Grams" type="xs:decimal" minOccurs="0" />
<xs:element name="Qty" type="xs:decimal" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:IngredientTable" />
<xs:field xpath="mstns:Id" />
</xs:unique>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<FatForWeightLossDataSet xmlns="http://tempuri.org/FatForWeightLossDataSet.xsd">
<IngredientTable diffgr:id="IngredientTable1" msdata:rowOrder="0">
<Id>1</Id>
<Name>Natural Yoghurt</Name>
<Calories>103.00</Calories>
<Carbs>7.00</Carbs>
<Fat>6.00</Fat>
<Protein>0.00</Protein>
<Cholesterol>0.00</Cholesterol>
<Sodium>0.00</Sodium>
<Sugars>0.00</Sugars>
<Fibre>0.00</Fibre>
<Grams>100.00</Grams>
</IngredientTable>
您引用了错误的元素集。您的代码引用IngredientTableDataTable
,其中不在名称空间中。您要查找的数据似乎在IngredientTable
节点中,所有这些都在名称空间中。你应该这样做:
XNamespace ns = "http://tempuri.org/FatForWeightLossDataSet.xsd";
var ingredients =
from i in doc.Descendants(ns + "IngredientTable")
select new
{
Id = (int)i.Element(ns + "Id"),
Name = (string)i.Element(ns + "Name"),
Calories = (decimal)i.Element(ns + "Calories"),
Carbs = (decimal)i.Element(ns + "Carbs"),
Fat = (decimal)i.Element(ns + "Fat"),
Protein = (decimal)i.Element(ns + "Protein"),
Cholesterol = (decimal)i.Element(ns + "Cholesterol"),
Sodium = (decimal)i.Element(ns + "Sodium"),
Sugars = (decimal)i.Element(ns + "Sugars"),
Fibre = (decimal)i.Element(ns + "Fibre"),
Grams = (decimal)i.Element(ns + "Grams"),
};