无法反序列化XML文档(内部异常:<arrayofstring xmlns =“&gt;不是预期的)

时间:2018-05-10 17:10:09

标签: c# .net xml

&lt; p&gt;我编写了一个序列化例程,该例程采用3个独立的流,并将来自三个不同阵列的字符串数据序列化为相应的XML文件。 但是,在反序列化时,我遇到了一个例外:&lt; / p&gt; &lt; p&gt;不期望ArrayOfString xmlns ........&lt; / p&gt; &lt; p&gt;目前我对XML的了解有限,但我知道我的XML文件顶部有一个特定的声明,我认为这会给反序列化带来麻烦:&lt; / p&gt; &lt; p&gt;以下是一些反序列化代码:&lt; / p&gt; &LT;预&GT;&LT;代码&GT; //创建一个字符串数组来保存反序列化的数据。         string [] stringArray = new string [10];         //找到存储任务信息的XML文件的目标文件路径。         string taskCategoryFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory).ToString()+“taskcategory.txt”;&#xa; =“”try&#xa; =“”{&#xa; =“” using =“”(streamreader =“”reader =“new”streamreader(taskcategoryfilepath))&#xa; =“”{&#xa; =“”declare =“”a =“”new =“”xml =“” document =“”和=“”serializer =“”object。&#xa; =“”xmldocument =“”document =“new”xmldocument();&#xa; =“”xmlserializer =“”serializer =“new” xmlserializer(typeof(string));&#xa;&#xa; =“”attempt =“”to =“”assign =“”the =“”deserialized =“”data =“”to =“”a =“ “string =”“array。&#xa; =”“stringarray =”(string [])serializer.Deserialize(reader); “reader.close();&#xa; =”“}&#xa; =”“}&#xa; =”“catch(exception =”“ex)&#xa; =”“{&#xa; =“”messagebox.show(“there =”“was =”“an =”“error =”“loading =”“the =”“tasks:”=“”+ =“”ex.tostring());& #xa; =“”} =“”&#xa; =“”}&#xa; <=“”code =“”>

我认为我可以通过某些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事件)    ---内部异常堆栈跟踪结束 -

2 个答案:

答案 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);