2011-12-06 48 views
2

这是我尝试反序列化的XML文件:无法反序列化只有一个节点的XML文件;根节点

<?xml version="1.0" encoding="utf-8"?> 
<d:MyItem xmlns:d="http://someurl" xmlns:m="http://someotherurl">This is a string</d:MyItem> 

xsd tool生成以下类:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://someurl")] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://someurl", IsNullable=false)] 
public partial class MyItem { 

    private object[] itemsField; 

    /// <remarks/> 
    public object[] Items { 
     get { 
      return this.itemsField; 
     } 
     set { 
      this.itemsField = value; 
     } 
    } 
} 

我目前正试图反序列化相同的XML xsd用于生成类:

var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<d:MyItem xmlns:d=\"http://someurl\" xmlns:m=\"http://someotherurl\">This is a string</d:MyItem>"; 
var deserialized = Deserialize<MyItem>(xml); 

其中Deserialize<>是:

private static T Deserialize<T>(string xml) 
{ 
    var xmlDocument = XDocument.Parse(xml); 
    var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T)); 
    return (T)serializer.Deserialize(xmlDocument.CreateReader()); 
} 

的问题是,虽然Deserialize返回一个实例(不为空),它里面的属性Itemsnull即它没有被反序列化。

我怎样才能从这个XML里面得到字符串?

回答

0

你的xml看起来不正确,尝试序列化这个类的一个实例,并看看生成的xml。我认为,必须有像

<?xml version="1.0" encoding="utf-8"?> 
<d:MyItem xmlns:d="http://someurl" xmlns:m="http://someotherurl"> 
<ArrayOfItems> 
<Item>...</Item> 
... 
</ArrayOfItems> 
</d:MyItem> 

如果要反序列化的XML,你应该有一个类

public partial class MyItem { 

/// <remarks/> 
public String Item{ get; set; } 
} 

,并在XML

<?xml version="1.0" encoding="utf-8"?> 
<d:MyItem xmlns:d="http://someurl" xmlns:m="http://someotherurl"> 
<Item>some text</Item>  
</d:MyItem> 
+0

但是是不是有你如何可以序列我张贴的XML的方法吗? –

+0

其实你可以。尝试这样的事情 [XmlRoot(的ElementName = “项目”,数据类型= “字符串”) 公共部分类MyItem { /// 公共字符串项{获取;集;}} – Eugene

+0

感谢您的建议,但它仍然没有工作。 –

0

有问题,你XML,如果你使用这个XML,那么你可以使用你的代码使之无效化。

var xml = "<MyItem xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://someurl\"> <Items> <anyType xsi:type=\"xsd:string\">This is string</anyType> </Items></MyItem>"; 

这会给你从你的XML内的串

3

XSD.EXE期待您的文档根元素是一个复杂的类型,但在你的情况下,它是一个简单的字符串,所以内各种假设XSD.exe导致问题。它产生的错误模式只是几个问题中的第一个。

最简单的办法是忽略XSD.EXE和刚刚创建自己的XML序列化类:

[System.SerializableAttribute()] 
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://someurl")] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://someurl", IsNullable = false)] 
public partial class MyItem 
{ 
    [System.Xml.Serialization.XmlTextAttribute] 
    public string Value { get; set; } 
} 

而且,我不知道你为什么在反序列化使用XDocument.Parse。你可以把它简化这样的:

private static T Deserialize<T>(string xml) 
{ 
    var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T)); 
    return (T)serializer.Deserialize(new StringReader(xml)); 
} 

下面是完整的工作代码:

using System; 
using System.IO; 
using System.Xml; 
using System.Xml.Serialization; 

namespace ConsoleApplication1 
{ 
    [System.SerializableAttribute()] 
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://someurl")] 
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://someurl", IsNullable = false)] 
    public partial class MyItem 
    { 
     [System.Xml.Serialization.XmlTextAttribute] 
     public string Value { get; set; } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<d:MyItem xmlns:d=\"http://someurl\" xmlns:m=\"http://someotherurl\">This is a string</d:MyItem>"; 
      var deserialized = Deserialize<MyItem>(xml); 
      // Result: deserialized.Value == "This is a string" 
     } 

     private static T Deserialize<T>(string xml) where T : new() 
     { 
      var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T)); 
      return (T)serializer.Deserialize(new StringReader(xml)); 
     } 
    } 
}