2009-06-16 64 views
1

我有以下代码:我的XML错误?

public class DeserializeAndCompare 
{ 
    public static List<string> IntoXML() 
    { 
     List<string> PopList = new List<string>(); 

     XmlSerializer serializer = new XmlSerializer(PopList.GetType()); 
     string k = FileToolBox.position0; 
     FileStream filestreamer = new FileStream(k.ToString(), FileMode.Open); 
     PopList = (List<string>)serializer.Deserialize(filestreamer); 
     filestreamer.Close(); 
     return PopList; 

    } 
} 

我不断撞击与线路的误差: 弹出式列表=(列表)serializer.Deserialize(filestreamer);

错误:InvalidOperationException未处理,XML文档(1,1)中存在错误。

在这一行: FileStream filestreamer = new FileStream(k,FileMode.open);

我想引用包含字符串的数组的第0个位置。我基本上是通过我的目录,找到任何扩展名为.xml的文件,并在数组中保存文件名路径。
这里是我的数组代码:

public static class FileToolBox 
{ 

    public static string position0; 
    public static void FileSearch() 
    { 



     //string position0; 

     //array holding XML file names 
     string[] array1 = Directory.GetFiles(@"s:\project", "*.xml"); 

     Array.Sort(array1); 
     Array.Reverse(array1); 
     Console.WriteLine("Files:"); 
     foreach (string fileName in array1) 
     { 

      Console.WriteLine(fileName); 

     } 

     position0 = array1[0]; 

    } 

    public static string Position0 
    { 
    get 
     { 
      return position0; 
     } 
     set 
     { 
      position0 = value; 
     } 

    } 
    } 

我在这里失去了一些东西?我如何摆脱这个错误?

在此先感谢您的帮助。

+0

啊谢谢大家!这确实是我的实际XML文件错误...不能相信我最初没有捕捉到>< – yeahumok 2009-06-16 18:12:28

回答

3

您的XML文件格式不正确,请使用XML Spy,XML记事本等工具或在IE中打开它,它会给您提供错误和线路。你最有可能有无效字符,如在文件中&地方

1

该错误明确指出正在读取的XML文件格式错误。您应该首先发布您的XML。另外,尝试在Firefox中打开XML,因为它也可能指出XML的问题。

1

您的xml文档格式不正确,您需要打开您的xml文件并进行分析。

网络上有多个xml验证程序,但是这里有一个来自w3schools

0

给这一个镜头:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Text; 
using System.Xml; 
using System.Xml.Serialization; 

namespace Util 
{ 
    /// <summary> 
    /// Not to be confused with System.Xml.Serialization.XmlSerializer, which this uses internally. 
    /// 
    /// This will convert the public fields and properties of any object to and from an XML string, 
    /// unless they are marked with NonSerialized() and XmlIgnore() attributes. 
    /// </summary> 
    public class XMLSerializer 
    { 
     public static Byte[] GetByteArrayFromEncoding(Encoding encoding, string xmlString) 
     { 
      return encoding.GetBytes(xmlString); 
     } 

     public static String SerializeToXML<T>(T objectToSerialize) 
     { 
      return SerializeToXML(objectToSerialize, Encoding.UTF8); 
     } 

     public static String SerializeToXML<T>(T objectToSerialize, Encoding encoding) 
     { 
      StringBuilder sb = new StringBuilder(); 

      XmlWriterSettings settings = 
       new XmlWriterSettings { Encoding = encoding, Indent = true }; 

      using (XmlWriter xmlWriter = XmlWriter.Create(sb, settings)) 
      { 
       if (xmlWriter != null) 
       { 
        new XmlSerializer(typeof (T)).Serialize(xmlWriter, objectToSerialize); 
       } 
      } 

      return sb.ToString(); 
     } 

     public static void DeserializeFromXML<T>(string xmlString, out T deserializedObject) where T : class 
     { 
      DeserializeFromXML(xmlString, new UTF8Encoding(), out deserializedObject); 
     } 

     public static void DeserializeFromXML<T>(string xmlString, Encoding encoding, out T deserializedObject) where T : class 
     { 
      XmlSerializer xs = new XmlSerializer(typeof(T)); 

      using (MemoryStream memoryStream = new MemoryStream(GetByteArrayFromEncoding(encoding, xmlString))) 
      { 
       deserializedObject = xs.Deserialize(memoryStream) as T; 
      } 
     } 
    } 
} 


public static void Main() 
{ 
    List<string> PopList = new List<string>{"asdfasdfasdflj", "asdflkjasdflkjasdf", "bljkzxcoiuv", "qweoiuslfj"}; 

    string xmlString = Util.XMLSerializer.SerializeToXML(PopList); 

    XmlDocument xmlDoc = new XmlDocument(); 
    xmlDoc.LoadXml(xmlString); 

    string fileName = @"C:\temp\test.xml"; 
    xmlDoc.Save(fileName); 

    string xmlTextFromFile = File.ReadAllText(fileName); 

    List<string> ListFromFile; 

    Util.XMLSerializer.DeserializeFromXML(xmlTextFromFile, Encoding.Unicode, out ListFromFile); 

    foreach(string s in ListFromFile) 
    { 
     Console.WriteLine(s); 
    } 
} 

检查输出XML文件,看看编码是什么,和比较,为正在试图编码你什么读入。之前我遇到过这个问题,因为我使用StringBuilder输出以UTF-16写入的XML字符串,但我试图以UTF-8读入。尝试使用Encoding.Unicode,看看是否适合你。

0

您的代码将只能与具有以下结构的XML文件的工作......

<?xml version="1.0" encoding="utf-16"?> 
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <string>Hello</string> 
    <string>World</string> 
</ArrayOfString>