c# - 使用 Asp-C 写入 Xml 文件#

标签 c# asp.net xml

我正在尝试将一些值存储到一个 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);
            }

}

关于c# - 使用 Asp-C 写入 Xml 文件#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13192378/

相关文章:

c# - MongoDB C# Driver 2.1.0 - 解析引用

c# - 如何在运行时更改资源字典颜色

html - asp.CheckBox 渲染显示在跨度内。样式属性以跨度结束,而不是复选框

javascript - 使用 moment.js 转换 XML 解析时间和日期的时区

c# - 使用 C# 中的 odp.net 和 OCI 连接到 Oracle

c# - .NET 的离散事件模拟框架

xml - 每个命名空间都有一个 targetNamespace 吗?

javascript - 使用http打开本地html页面进行测试

c# - 如何从 Web API 应用程序返回 PDF

javascript - 如何从 <asp :textbox> to javascript function 传递值