简体   繁体   English

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

[英]How to write data in xml file wp8

I have add one xml file in Assets folder This is Empty and when i try to add list data in xml file this return a error. 我在Assets文件夹中添加了一个xml文件,这是空的,当我尝试在xml文件中添加列表数据时,这将返回错误。 error show in images please check the image . 图片中显示错误,请检查图片。 here i have attach a image . 在这里我附上一张图片。 Please Help me This is my code 请帮我这是我的代码

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);
            }
        }
    }

This is my class 这是我的课

 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; }
    }
}

} }

Here i attach a error image in visual studio 在这里我在Visual Studio中附加了错误图像 在此处输入图片说明

As the error message suggested, Bookmarkdata class is inaccessible due to it's protection level. 如错误消息所示,由于具有保护级别,因此无法访问Bookmarkdata类。 You need to simply set it's access modifier to public : 您只需将其访问修饰符设置为public即可:

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

and XmlSerializer will be able to serialize your list smoothly :) XmlSerializer将能够顺利地序列化您的列表:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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