如何在xml文件wp8中写入数据

时间:2014-03-11 10:53:41

标签: c# xml windows-phone-8

我在Assets文件夹中添加了一个xml文件。这是空的 当我尝试在xml文件中添加列表数据时,返回错误。错误显示在图像中请检查图像。在这里我附上了一张图片。请帮我 这是我的代码

public void BookmarkPage()
    {
        List<Bookmarkdata> data = new List<Bookmarkdata>();
        data.Add(new Bookmarkdata() { Bookname = "Kate", Bookid = "Brown", BookPath = "Brown", Pageno = 25 });
        data.Add(new Bookmarkdata() { Bookname = "Tom", Bookid = "Stone", BookPath = "Brown", Pageno = 63 });
        data.Add(new Bookmarkdata() { Bookname = "Michael", Bookid = "Liberty", BookPath = "Brown", Pageno = 37 });
        AddXml(data);

    }

private void AddXml(List<Bookmarkdata> data)
{

    XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
    xmlWriterSettings.Indent = true;
    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("BookmarkFile.xml", FileMode.Create))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Bookmarkdata>));
            using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
            {
                serializer.Serialize(xmlWriter, data);
            }
        }
    }

这是我的班级

 class Bookmarkdata
{
    string bookname;
    string bookid;
    string bookPath;
    int pageno;


    public string Bookname
    {
        get { return bookname; }
        set { bookname = value; }
    }

    public string Bookid
    {
        get { return bookid; }
        set { bookid = value; }
    }
    public string BookPath
    {
        get { return bookPath; }
        set { bookPath = value; }
    }

    public int Pageno
    {
        get { return pageno; }
        set { pageno = value; }
    }
}

}

这里我在visual studio中附加了一个错误图像 enter image description here

1 个答案:

答案 0 :(得分:0)

正如错误消息所示,Bookmarkdata类由于其保护级别而无法访问。您只需将其访问修饰符设置为public:

public class Bookmarkdata
{
    .........
    .........
}

XmlSerializer将能够顺畅地序列化您的列表:)