【问题标题】:Write to Xml File using Asp-C#使用 Asp-C# 写入 Xml 文件
【发布时间】:2012-11-02 09:17:06
【问题描述】:

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

但它不起作用..

根级别的数据无效。第 1 行,位置 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.

为什么会这样?

【问题讨论】:

  • LoadXml 用于加载表示为文本的 xml 文档。您想改用 Load(如注释行中所示)
  • 缺少根元素现在发生此错误
  • 表示您的文档为空。只需做一个new XmlDocument() 然后
  • 就像西蒙说的,Load 是你应该使用的方法。你能发布你得到的完整错误信息以及你在哪里得到它吗?另外,请尝试查看this question
  • @Sudheesh 你的 xml 文件是空的..你检查过 xml 是否为空或是否包含与上图相同的格式..检查 xml 文件和你所指的路径..跨度>

标签: c# asp.net xml


【解决方案1】:

尝试使用 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);
            }

}

【讨论】:

    【解决方案2】:

    当您加载您的 Xml 时,您需要使用 Server.MapPath 。 XmlDoc.LoadXml(Server.MapPath("Password.xml"));

    【讨论】:

      【解决方案3】:

      删除

      <?xml version="1.0" encoding="utf-8"?>
      

      来自

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

      我不知道它为什么会起作用,但我们一直在代码中这样做。是的,您应该使用XmlDocument.Load 而不是XmlDocument.LoadXml

      【讨论】:

        猜你喜欢
        • 2017-06-12
        • 1970-01-01
        • 2013-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-15
        • 1970-01-01
        相关资源
        最近更新 更多