XML问题。 [英] XML issues.

查看:94
本文介绍了XML问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试创建并写入特定文件夹中的xml文件。我知道有更好的方法来保存数据库,但这是我选择这样做的方式。话虽如此。我遇到了一个问题,我的服务器一直告诉我
缺少Root元素或文件已经在使用中。那么我该如何:


A:完成后关闭文件。


B:实际写入文件并能够稍后关闭和/或重新打开它以读取数据。 

 public static bool createAccount(string userName,string passWord)
{
string userFolder = folder +" \\" + userName;
if(!Directory.Exists(userFolder))
{
try {
Directory.CreateDirectory(userFolder);
XmlDocument doc = new XmlDocument();
doc.Load(File.Create(userFolder +" \\Account.xml"));
XmlNode root = doc.CreateElement(" Account");
doc.DocumentElement.AppendChild(root);

XmlNode user = doc.CreateElement(" Username");
user.InnerText = userName;
root.AppendChild(user);

XmlNode pass = doc.CreateElement(" Password");
pass.InnerText = Encryption.Encrypt(passWord);
root.AppendChild(pass);
Console.WriteLine(" Account Created");
返回true;
}
catch(例外e)
{
Console.WriteLine(e.Message);
返回false;
}
}
其他
{
返回false;
}
}

它一直告诉我"根元素缺失。"我已经尝试过多次研究这个问题,但似乎找不到任何可行的答案。代码应该产生如下代码:

< Account> 
<用户名>用户< /用户名>
<密码>传递< /密码>
< /帐户>

编辑:+++++++++++ +++++++++++++++++++++++++++++++++++系统好一点。这是我的解决方案:


创建文件时。它只创建了一个纯文本文件。我不确定是否存在自动创建"根元素"的功能。但这就是我做到的。 

 public static bool createAccount(string userName,string passWord)
{
string userFolder = folder +"\\" + userName +" \\" ;;
if(!Directory.Exists(userFolder))
{
#region创建文件
Directory.CreateDirectory(userFolder);
XmlDocument doc = new XmlDocument();
File.Create(userFolder +" Account.xml")。Close();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars =" / t" ;;
XmlWriter writer = XmlWriter.Create(userFolder +" Account.xml");
writer.WriteStartDocument(true);
writer.WriteStartElement(" Account");
writer.WriteAttributeString(" LoggedIn"," false");
writer.WriteEndElement();
writer.Close();
#endregion

doc.Load(userFolder +" Account.xml");

#region创建信息
XmlNode root = doc.DocumentElement;
XmlNode user = root.AppendChild(doc.CreateElement(" Username"));
user.InnerText = userName;
XmlNode pass = root.AppendChild(doc.CreateElement(" Password"));
pass.InnerText = Encryption.Encrypt(passWord);
doc.Save(userFolder +" Account.xml");
#endregion
返回true;
}
其他
{
返回false;
}
}



解决方案

XML根元素是技术作者不解释的东西,因为他们假设你已经知道它。我没有在MSDN中的任何地方看到解释,但我确信它在MSDN中的某处解释过。请参阅
XML语法;它说"XML文档必须包含一个
root 元素,它是所有其他元素的"。所以有时候我们必须为根元素组成一个名字。在您的简短示例中,"帐户"是根,必须只有一个。如果您有多个帐户
,那么您可以设置根"帐户" (复数)然后有多个"帐户" (单数)元素。


要重新打开文件,我认为你需要添加一个明确的"Dispose"。在"关闭"之后。


使用XML,您需要读取整个文件,然后处理它,然后写出整个文件。我认为不支持将数据插入现有的XML文件,也不能在适当的位置更新。您可以写入现有的XML文件
但是如果您创建了一个新文件,那么它也可以提供备份机制。因此,您可以在创建新文件之后重命名先前版本,然后重命名新文件。


Hi I'm trying to create and write to an xml file in a specific folder. I know there are better ways to keep a database but this is the way that i'm choosing to do it. with that being said. I'm running into an issue where my server keeps telling me that there are Root elements missing or that the file is already in use. So how do I:

A: Close the file after I'm finished with it.

B: Actually write into the file and be able to close and/or re-open it later to read the data. 

public static bool createAccount(string userName, string passWord)
        {
            string userFolder = folder + "\\" + userName;
            if (!Directory.Exists(userFolder))
            {
                try {
                    Directory.CreateDirectory(userFolder);
                    XmlDocument doc = new XmlDocument();
                    doc.Load(File.Create(userFolder + "\\Account.xml"));
                    XmlNode root = doc.CreateElement("Account");
                    doc.DocumentElement.AppendChild(root);

                    XmlNode user = doc.CreateElement("Username");
                    user.InnerText = userName;
                    root.AppendChild(user);

                    XmlNode pass = doc.CreateElement("Password");
                    pass.InnerText = Encryption.Encrypt(passWord);
                    root.AppendChild(pass);
                    Console.WriteLine("Account Created");
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

It keeps telling me "Root element is missing." I've tried researching this issue multitude of times and can't seem to find any viable answer. The code should produce something like this:

<Account>
     <Username>user</Username>
     <Password>pass</Password>
</Account>

Edit: ++++++++++++++++++++++++++++++

I found a temporary workaround for now until i understand the system a little better. Here is my solution:

when creating the file. it created just a plain text file. I'm not sure if there is a function that automatically creates a "Root Element" but this is how i did it. 

        public static bool createAccount(string userName, string passWord)
        {
            string userFolder = folder + "\\" + userName + "\\";
            if (!Directory.Exists(userFolder))
            {
                #region Creating file
                Directory.CreateDirectory(userFolder);
                XmlDocument doc = new XmlDocument();
                File.Create(userFolder + "Account.xml").Close();
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.IndentChars = "/t";
                XmlWriter writer = XmlWriter.Create(userFolder + "Account.xml");
                writer.WriteStartDocument(true);
                writer.WriteStartElement("Account");
                writer.WriteAttributeString("LoggedIn", "false");
                writer.WriteEndElement();
                writer.Close();
                #endregion

                doc.Load(userFolder + "Account.xml");

                #region Creating Information
                XmlNode root = doc.DocumentElement;
                XmlNode user = root.AppendChild(doc.CreateElement("Username"));
                user.InnerText = userName;
                XmlNode pass = root.AppendChild(doc.CreateElement("Password"));
                pass.InnerText = Encryption.Encrypt(passWord);
                doc.Save(userFolder + "Account.xml");
                #endregion
                return true;
            }
            else
            {
                return false;
            }
        }


解决方案

XML root elements are the type of thing that technical writers don't explain because they assume you already know it. I don't see an explanation anywhere in the MSDN but I am sure it is explained somewhere in the MSDN. So see XML Syntax; it says "XML documents must contain one root element that is the parent of all other elements". So sometimes we must make up a name for a root element. In your short example, "Account" is the root and there must be only one. If you have multiple accounts then you can make your root "Accounts" (plural) and then have multiple "Account" (singular) elements.

To re-open the file I think you need to add an explicit "Dispose" after the "Close".

With XML, you need to read the entire file in, then process it, then write the entire file out. I don't think there is support for inserting data into an existing XML file nor can we update in place. You might be able to write over the existing XML file but if you create a new file then that can provide a backup mechanism too. So you could do a rename of prior versions after creating a new file and then a rename of the new file.


这篇关于XML问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆