簡體   English   中英

將XML文檔反序列化為C#對象集合

[英]deserialize XML document to C# collection of objects

我需要一些幫助以指出我在這里做錯了什么。 我已經搜索了SO,並嘗試了不同的方式來加載此XML,但我只是看不到是什么原因導致了此錯誤:

System.InvalidOperationException:XML文檔(1,1)中存在錯誤。 ---> System.Xml.XmlException:根級別的數據無效。 第1行,位置1。

<?xml version="1.0" encoding="utf-8"?>
<folderlist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<foldersetting>
  <id>1</id>
  <source>
    <path>\\USApps2\AVL\Attachments</path>
    <datelastread>2016-01-25T10:51:12.6030162-08:00</datelastread>
    <filter>
      <owner>US\</owner>
      <filetype>*</filetype>
    </filter>
  </source>
  <destination>
    <path>E:\UserData\AVL</path>
    <overwrite>false</overwrite>
  </destination>
</foldersetting>
<foldersetting>
  <id>2</id>
  <source>
    <path>\\TWAVLSVR\AVL\Attachments</path>
    <datelastread>2016-01-22T10:51:12.6030162-08:00</datelastread>
    <filter>
      <owner>TW\</owner>
      <filetype>PDF</filetype>
    </filter>
  </source>
  <destination>
    <path>E:\UserData\AVL</path>
    <overwrite>false</overwrite>
  </destination>
</foldersetting>
<foldersetting>
  <id>3</id>
  <source>
    <path>E:\UserData\AVL</path>
    <datelastread>2016-01-22T10:51:12.6030162-08:00</datelastread>
    <filter>
      <owner>US\</owner>
      <filetype>*</filetype>
    </filter>
  </source>
  <destination>
    <path>E:\UserData\AVL\Web</path>
    <overwrite>false</overwrite>
  </destination>
</foldersetting>
</folderlist>

這是我的班級定義:

[Serializable()]
public class Filter
{
    [XmlElement("owner")]
    public string Owner {get; set;}

    [XmlElement("filetype")]
    public string FileType { get; set; }
}

[Serializable()]
public class SourceFolder
{
    [XmlElement("path")]
    public string Path { get; set; }

    [XmlElement("datelastread")]
    public DateTime DateLastRead { get; set; }

    [XmlElement("filter")]
    public Filter FilterTypes { get; set; }
}

[Serializable()]
public class DestinationFolder
{
    [XmlElement("path")]
    public string Path { get; set; }

    [XmlElement("overwrite")]
    public bool OverWrite { get; set; }
}

[Serializable()]
public class FolderSetting
{
    [XmlElement("id")]
    public int ID { get; set; }

    [XmlElement("source")]
    public SourceFolder Source { get; set; }

    [XmlElement("destination")]
    public DestinationFolder Destination { get; set; }
}

[Serializable()]
[XmlRoot("folderlist")]
public class FolderList
{
    public FolderList()
    {
        FolderSettings = new List<FolderSetting>();
    }

    [XmlElement("foldersetting")]
    public List<FolderSetting> FolderSettings;
}

在這里,我反序列化XML:

XmlSerializer serializer = new XmlSerializer(typeof(FolderList));
using (StringReader reader = new StringReader("C:\\Folders.xml"))
{
    FolderList folders = (FolderList)(serializer.Deserialize(reader));
}

我在這里遵循了一個建議,以填充我擁有的類並對其進行序列化,以確保獲得適用於我的類的正確格式,但這仍然無濟於事。 希望有人能看到我在這里所缺少的。

問題是在反序列化中使用StringReader時,應使用StreamReader:

using (StreamReader reader = new StreamReader("D:\\Folders.xml"))
{
  FolderList folders = (FolderList)(serializer.Deserialize(reader));
}

因此,結果是序列化程序正在嘗試反序列化“ D:\\ Folders.xml”,而不是OP試圖反序列化的數據文件。

您的XML缺少</ folderlist>結束標記。

您可以在此處驗證XML: http//www.xmlvalidation.com/

作為一種個人編碼樣式,請考慮從一般類開始學習您的類,然后轉向特定類。 當您首先看到頂層對象時,閱讀起來會更容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM