[英]I'm trying to simply append two different data types into my xml file and read them again, but I am getting an error
I'm using Winforms and I get the following error when I press the load button after having hit the save button:我正在使用 Winforms,在按下保存按钮后按下加载按钮时出现以下错误:
InvalidOperationException: was not expected.
InvalidOperationException:不是预期的。
Here is my code:这是我的代码:
private void save_Click(object sender, EventArgs e)
{
// if you don't specify it, then it saves in the bin folder of the program
FileStream fs = new FileStream(@"C:\Users\Owner\Desktop\Student2.Xml", FileMode.Append, FileAccess.Write); //@"C:\Users\Owner\Desktop\Student.Xml"
StudentClass sc = new StudentClass();
sc.Name = textBox1.Text;
sc.Class = int.Parse(textBox2.Text);
ls.Add(sc);
StudentSerializer.Serialize(fs, ls);
DicSerializer.Serialize(fs,
dict.Select(kv => new DicItem() { id = kv.Key, value = kv.Value }).ToArray());
fs.Close();
}
private void load_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"C:\Users\Owner\Desktop\Student2.Xml", FileMode.Open, FileAccess.Read);
ls = (List<StudentClass>)StudentSerializer.Deserialize(fs);
var orgDict = ((DicItem[])DicSerializer.Deserialize(fs))
.ToDictionary(i => i.id, i => i.value);
foreach (var item in orgDict)
Debug.WriteLine(item.Key + " and " + item.Value);
fs.Close();
}
My dictionary already has two items in it.我的字典里已经有两个项目了。 And my student class has two items as well from the textboxes.
我的学生 class 在文本框中也有两个项目。 Also note, my xml file gets wiped clean after that error.
另请注意,我的 xml 文件在该错误后被擦除干净。 Any help is appreciated thanks.
感谢任何帮助。
Also, this is what my xml looks like right after hitting the save button:此外,这是我的 xml 在点击保存按钮后的样子:
<?xml version="1.0"?>
<ArrayOfStudentClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<StudentClass>
<Name>wewewe</Name>
<Class>2222</Class>
</StudentClass>
</ArrayOfStudentClass><?xml version="1.0"?>
<DicItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DicItem id="1" value="one" />
<DicItem id="2" value="two" />
</DicItem>
The complaint is because of <?xml version="1.0"?>
right before the first DictItem tag.投诉是因为
<?xml version="1.0"?>
就在第一个 DictItem 标记之前。 Such tag AFAIK should appear once only in the XML file.此类标签 AFAIK 应仅在 XML 文件中出现一次。
You should post the Deserializer code for each class for more investigation.您应该发布每个 class 的解串器代码以进行更多调查。
If you remove the second declaration than following code will work如果您删除第二个声明,则以下代码将起作用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
XmlReader reader = XmlReader.Create(FILENAME, settings);
reader.ReadToFollowing("ArrayOfStudentClass");
XElement ArrayOfStudentClass = (XElement)XElement.ReadFrom(reader);
reader.ReadToFollowing("DicItem");
XElement DicItem = (XElement)XElement.ReadFrom(reader);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.