C# xml导入导入赋值给bindinglist或者list的问题,或者reader跳开<?xml version="1.0" ?>的方法
BindingList<Class1>cla=newBindingList<Class1>();Class1cla1=newClass1(5,"dsf");cla.Add...
BindingList<Class1> cla = new BindingList<Class1>();
Class1 cla1 = new Class1(5,"dsf");
cla.Add(cla1);
Class1 cla2 = new Class1(1, "ds");
cla.Add(cla2);
using (System.IO.StringWriter stringWriter = new StringWriter(new StringBuilder()))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(BindingList<Class1>));
xmlSerializer.Serialize(stringWriter, cla);
FileStream fs = new FileStream("a.xml", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.Write(stringWriter.ToString());
sw.Close();
fs.Close();
MessageBox.Show("写入文件成功!");
}
这是我写的bindinglist导出成xml文件的代码
xml的内容是:
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfClass1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Class1>
<Id>5</Id>
<Values>dsf</Values>
</Class1>
<Class1>
<Id>1</Id>
<Values>ds</Values>
</Class1>
</ArrayOfClass1>
然后我要把xml重新导入赋值给bindinglist,怎么写?
我试过用xmlreader,不过它不能识别
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfClass1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
,把他当成一个节点了,我删除了就成功了,不知道怎么改,求各位帮帮忙了,或者跳开这个节点读取的办法都可以,求完整代码。 展开
Class1 cla1 = new Class1(5,"dsf");
cla.Add(cla1);
Class1 cla2 = new Class1(1, "ds");
cla.Add(cla2);
using (System.IO.StringWriter stringWriter = new StringWriter(new StringBuilder()))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(BindingList<Class1>));
xmlSerializer.Serialize(stringWriter, cla);
FileStream fs = new FileStream("a.xml", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.Write(stringWriter.ToString());
sw.Close();
fs.Close();
MessageBox.Show("写入文件成功!");
}
这是我写的bindinglist导出成xml文件的代码
xml的内容是:
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfClass1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Class1>
<Id>5</Id>
<Values>dsf</Values>
</Class1>
<Class1>
<Id>1</Id>
<Values>ds</Values>
</Class1>
</ArrayOfClass1>
然后我要把xml重新导入赋值给bindinglist,怎么写?
我试过用xmlreader,不过它不能识别
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfClass1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
,把他当成一个节点了,我删除了就成功了,不知道怎么改,求各位帮帮忙了,或者跳开这个节点读取的办法都可以,求完整代码。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询