Im currently trying to read an XML file and populate an observable collection, but I cant see what im doing wrong here, every time I look in the reader the xml nodes are empty
This code loads the XML file into the Xelement and I can see the data is correct.
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);
This seems to be where things are going wrong for me:
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);
}
If anyone can point me in the right direction id be really gratefull. thanks.
Edit: Here is my XML file
<?xml version="1.0" encoding="utf-8" ?>
<Cookies>
<Cookie>
<Name>Almond Cookies</Name>
<Description>Cookies With Almonds</Description>
<Ingredients>
<I开发者_JAVA技巧ngredient 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>
Eww, you're mixing LINQ to XML with regular old XML stuff. Choose one or the other, not both. Choose LINQ to XML. You've already got it started. Though you should use an XDocument
instead.
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
foreach (XElement cookie in appDataXml.Elements())
{
ViewModels.Cookie tempCookie = new ViewModels.Cookie();
tempCookie.Name = cookie.Element("Name").Value;
_cookieList.Add(tempCookie);
}
精彩评论