2009-06-16 55 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>