简体   繁体   中英

How can I parse XML in WP7?

How to parser this xml content.

<Content>    
    <caption> Today Headline </caption>        
    <s1>        
        <name>6</name>
        <name>4</name>
        <name>4</name>        
    </s1>            
    <s2>        
        <name>3</name>
        <name>6</name>
        <name>0</name>        
    </s2>    
</Content>

Mycode:

date = (from story 
        in xmlParser.Descendants("s1")
        select new EspnViewModel
        {
            Category = story.Element("name").Value,

        }).ToList();
        return data;

I'm having a hard time trying to figure out how to parse everything out.

Why dont you use xmlParser.Descendants("name") instead?

EDIT:

var caption = xmlParser.Descendants("caption").First().InnerText;

var names = from story in xmlParser.Descendants("name") select new EspnViewModel { Category = story.InnerText }).ToList();

Note: I am writing this in haste, but you get the idea..

private List<yourclass> ReadList()
{
    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (myIsolatedStorage.FileExists("xmlName.xml"))
            {
                using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("xmlName.xml", FileMode.Open))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<your class>));
                    List<yourclass> data = (List<yourclass>)serializer.Deserialize(stream);
                    return data.ToList();
                }
            }
            else
            {
                return null;
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM