加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

使用Asp-C写入Xml文件#

发布时间:2020-12-16 09:37:50 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试将一些值存储到xml文件中.我已经创建了一个Xml文件并尝试覆盖数据.代码是给.. /*storepassword.cs *//using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;public class StorePassword{ public
我正在尝试将一些值存储到xml文件中.我已经创建了一个Xml文件并尝试覆盖数据.代码是给..

/*storepassword.cs *//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

public class StorePassword
{
    public StorePassword()
    {
    }
    public void  store(NewPassword nps)
    {
       XmlDocument XmlDoc = new XmlDocument();

       //XmlDoc.Load(@"Password.xml");
       XmlDoc.LoadXml("Password.xml");

       XmlNode root = XmlDoc.DocumentElement;
       XmlNode myNode1 = root.SelectSingleNode("UserName");
       XmlNode myNode2 = root.SelectSingleNode("PassWord");
       myNode1.Value = "sjn";
       myNode2.Value = "sjn123";
       XmlDoc.Save(@"Password.xml");
   }
}

//NewPassword.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class NewPassword
{

    public NewPassword()
    {

    }
    public string username{ get; set; }
    public string Password{ get; set; }
}

点击按钮..

NewPassword nps = new NewPassword();
nps.username = TxtUser.Text;
nps.Password = TxtNewPassword.Text;
StorePassword sp=new StorePassword();
sp.store(nps);

现有的Xml文件包含以下内容..

<?xml version="1.0" encoding="utf-8"?>
<ROOT>
  <UserName>abc</UserName>
  <PassWord>123</PassWord>
</ROOT>

但它不起作用..

Data at the root level is invalid. Line 1,position 1

发生此错误..

将代码更改为XmlDoc.Load(@“Password.xml”);

现在错误更改为

Root element is missing.

   Description: An unhandled exception occurred during the execution of the current web                  request. Please review the stack trace for more information about the error and where it   originated in the code. 

Exception Details: System.Xml.XmlException: Root element is missing.

为什么会这样?

解决方法

尝试使用XML序列化:

public static partial class ObjectXMLSerializer<T> where T : class
{
            private static void SaveToDocumentFormat(T serializableObject,System.Type[] extraTypes,string path,IsolatedStorageFile isolatedStorageFolder)
            {
                using (TextWriter textWriter = CreateTextWriter(isolatedStorageFolder,path))
                {
                    XmlSerializer xmlSerializer = CreateXmlSerializer(extraTypes);

                    //Cuong: set name space to null
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add("","");
                    xmlSerializer.Serialize(textWriter,serializableObject,ns);
                }
            }
            public static void Save(T serializableObject,string path)
            {
                    SaveToDocumentFormat(serializableObject,null,path,null);
            }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读