我认为我可以通过某些XML属性或属性从我的文件中删除有问题的XML声明,或者我可以以省略声明的违规部分的方式序列化数据。我如何做到这一点就是我被困在哪里 - 或者它可能是一个完全不同的问题!
编辑:这是序列化例程,以防它可能有用:
// Create 3 seperate memory streams, one for each
// file destination of the tasks.
MemoryStream stream1 = new MemoryStream();
MemoryStream stream2 = new MemoryStream();
MemoryStream stream3 = new MemoryStream();
// Create an XML document and serializer object.
XmlDocument xmlDocument = new XmlDocument();
XmlSerializer serializer = new XmlSerializer(this.TasksBody.GetType());
// Loop through this functionality 3 times,
// recursively serializing task category, name and body arrays.
for (int i = 0; i < 3; i++)
{
try
{
// Switch-Case through index values, writing to each respective stream for
// the relevant task content.
switch (i)
{
case 0:
serializer.Serialize(stream1, this.TasksCategory);
stream1.Position = 0;
xmlDocument.Load(stream1);
break;
case 1:
serializer.Serialize(stream2, this.TaskName);
stream2.Position = 0;
xmlDocument.Load(stream2);
break;
case 2:
serializer.Serialize(stream3, this.TasksBody);
stream3.Position = 0;
xmlDocument.Load(stream3);
break;
}
switch (i)
{
case 0:
xmlDocument.Save(taskCategoryFilePath);
stream1.Flush();
break;
case 1:
xmlDocument.Save(taskNameFilePath);
stream2.Flush();
break;
case 2:
xmlDocument.Save(taskBodyFilePath);
stream3.Flush();
break;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
break;
}
}
编辑:以下是XML输出文件信息的示例:
<?xml version="1.0"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>Songs to learn</string>
<string>Games to play</string>
编辑:这里也是内部异常:
System.InvalidOperationException:XML文档中存在错误(2,2)。 ---&GT; System.InvalidOperationException:不期望。 在System.Xml.Serialization.XmlSerializationPrimitiveReader.Read_string() 在System.Xml.Serialization.XmlSerializer.DeserializePrimitive(XmlReader xmlReader,XmlDeserializationEvents事件) 在System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,String encodingStyle,XmlDeserializationEvents事件) ---内部异常堆栈跟踪结束 -
答案 0 :(得分:2)
您的序列化程序类型不正确。
jQuery(document).ready(function ($) {
$(document).on("click", ".upload_image_button", function (e) {
e.preventDefault();
var $button = $(this);
// Create the media frame.
var file_frame = wp.media.frames.file_frame = wp.media({
title: 'Select or upload image',
library: { // remove these to show all
type: 'image' // specific mime
},
button: {
text: 'Select'
},
multiple: false // Set to true to allow multiple files to be selected
});
// When an image is selected, run a callback.
file_frame.on('select', function () {
// We set multiple to false so only get one image from the uploader
var attachment = file_frame.state().get('selection').first().toJSON();
$button.siblings('input').val(attachment.url);
});
// Finally, open the modal
file_frame.open();
});
});
应该是
XmlSerializer serializer = new XmlSerializer(typeof(string));
这是一个小小的演示示例
XmlSerializer serializer = new XmlSerializer(typeof(string[]));
答案 1 :(得分:1)
你的问题在这一行:
XmlSerializer serializer = new XmlSerializer(typeof(string));
应该是:
XmlSerializer serializer = new XmlSerializer(typeof(string[]));
目标对象是array
类型的string
,而不是string
。
另外,不要限制变量的大小:
var stringArray = (string[])serializer.Deserialize(reader);