【发布时间】: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