【问题标题】:Win phone 7 read XML fileWin phone 7 读取 XML 文件
【发布时间】:2011-06-12 02:44:29
【问题描述】:

我目前正在尝试读取一个 XML 文件并填充一个可观察的集合,但我看不到我在这里做错了什么,每次我查看阅读器时,xml 节点都是空的

这段代码将 XML 文件加载到 Xelement 中,我可以看到数据是正确的。

   StreamResourceInfo xml = Application.GetResourceStream(new Uri("/WindowsPhoneDataBoundApplication1;component/Cookies.xml", UriKind.Relative));

   XElement appDataXml;   
   // Get a stream to the XML file which contains the data and load it into the XElement. 
   appDataXml = XElement.Load(xml.Stream);

这似乎是我的问题所在:

    foreach (XNode n in appDataXml.Elements())
    {
        ViewModels.Cookie tempCookie = new ViewModels.Cookie();

        XmlReader reader = n.CreateReader();

        while (reader.Read())
        {
            if (reader.Name == "Name")
            {
                tempCookie.Name = reader.Value.ToString();
            }
        }
        _cookieList.Add(tempCookie);
    }

如果有人能指出我正确的方向,我真的很感激。 谢谢。

编辑:这是我的 XML 文件

<?xml version="1.0" encoding="utf-8" ?>
  <Cookies>
    <Cookie>
      <Name>Almond Cookies</Name>
      <Description>Cookies With Almonds</Description>
      <Ingredients>
        <Ingredient Name ="Unsalted Butter (Room Temperature)"
                    Amount="1 Cup" 
                    Alternate="2 Sticks"/>
        <Ingredient Name ="Granulated Sugar"
                    Amount="1 Cup" />
        <Ingredient Name ="Large Eggs"
                    Amount="2" />
        <Ingredient Name ="Canned Almond Filling"
                    Amount="1 Cup" />
        <Ingredient Name ="Whole Milk"
                    Amount="1/4 Cup" />
        <Ingredient Name ="All Purpose Flour"
                    Amount="3 Cups" />
        <Ingredient Name ="Baking Soda"
                    Amount="1/2 Teaspoon" />
        <Ingredient Name ="Salt"
                    Amount="1/4 Teaspoon" />
        <Ingredient Name ="Sliced Blanched Almonds"
                    Amount="1/4 Cup" />
      </Ingredients>
      <Temperature>
        <GasMark></GasMark>
        <C></C>
        <F>350</F>
      </Temperature>
    <Duration>15</Duration>
    <Instructions>
      <Line>Preheat the oven to 350F</Line>
      <Line>In a mixing bowl cream butter and sugar till smooth. Add Eggs and blend. Add almond filling and milk and Blend again.</Line>
      <Line>In a seperate bowl mix flour,baking soda and salt. Add to the creamed mixture.</Line>
      <line>Scoop 2 rounded tablespoons of dough and roll to form each cookie. Drop dough onto lightly greased or nonstick cookie sheets, spacing the cookies 2 inches apart. Press sliced almonds onto tops</line>
      <Line>Bake untill cookies are lightly golden and firm to touch, about 15 minutes. Using a spatula transfer cookies to rack and leave to cool.</Line>
    </Instructions>
    <MakesQty>28</MakesQty>
    <ContainsNuts>False</ContainsNuts>
  </Cookie>
</Cookies>

【问题讨论】:

  • 阅读器中的 XML 始终为空,只有元素但没有值,但是当我查看 Xelement 时,我可以正确看到 XML,
  • 能否分享您的 XML 文件的 sn-p,这可能会有所帮助
  • 完成了,我希望问题不在于 XML 文件,尽管好像我断点并实际查看 XElement 变量我可以正确看到所有值的 XML
  • 为什么要切换到阅读器来处理元素?

标签: c# xml silverlight windows-phone-7


【解决方案1】:

Eww,您将 LINQ to XML 与常规的旧 XML 内容混合在一起。选择其中之一,而不是两者都选。选择 LINQ to XML。你已经开始了。尽管您应该改用XDocument

var xmlUri = new Uri("/WindowsPhoneDataBoundApplication1;component/Cookies.xml",
                     UriKind.Relative);
var xmlStream = Application.GetResourceStream(xmlUri);

var doc = XDocument.Load(xmlStream);
var newCookies = doc
    .Descendants("Cookie")
    .Select(e =>
        new ViewModels.Cookie
        {
            Name = e.Element("Name").Value,
            // initialize any other values you need here            
            Description = e.Element("Description").Value,
            Ingredients = e
                .Element("Ingredients")
                .Elements("Ingredient")
                .Select(i =>
                    new Ingredient
                    {
                        Name = i.Element("Name").Value,
                        Amount = i.Element("Amount").Value,
                    }),
            Duration = (double)e.Element("Duration"),
            // etc.
        });
_cookieList.AddRange(newCookies); // add the cookies to our existing list

【讨论】:

  • “应该使用 XDocument 代替”:在 Xlinq 中这无关紧要,我什至怀疑是否推荐。
  • @Henk:也许吧。但是读进去的是一个完整的文档,所以他还不如把它当成一个文档来读。
【解决方案2】:
foreach (XElement cookie in appDataXml.Elements())
{
    ViewModels.Cookie tempCookie = new ViewModels.Cookie();
    tempCookie.Name = cookie.Element("Name").Value;

    _cookieList.Add(tempCookie);
}

【讨论】:

  • 我刚试过这个,cookiesRoot 抛出一个空引用异常:(
  • 所以直接试试appDataXml.Elements();
猜你喜欢
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-17
  • 2019-08-12
相关资源
最近更新 更多