从xml.Descendants加载数据会返回空值

时间:2016-10-26 14:31:28

标签: c# xml linq-to-xml

我已使用xml序列化程序将数据集内容导出到xml。在一个单独的项目(PCL项目)。我试图使用Xdocuments Descendants将xml文件读入对象类,但它没有返回任何值。

我错过了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>

1 个答案:

答案 0 :(得分:0)

您引用了错误的元素集。您的代码引用了名称空间中不是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"),
    };