-3
我有以下XML:地圖XML子元素的C#類
xml += "<?xml version=\"1.0\" encoding=\"UTF - 8\"?>";
xml += "<root>";
xml += " <success>true</success>";
xml += " <data>";
xml += " <item>";
xml += " <StateId>0</StateId>";
xml += " <StateName>Test</StateName>";
xml += " <GroupId>0</GroupId>";
xml += " </item>";
xml += " </data>";
xml += "</root>";
基本上我感興趣的是根 - >數據 - >項目唯一的元素,我試圖得到它:
public static T ConvertXmlToClass<T>(string xml)
{
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "root";
xRoot.IsNullable = true;
var serializer = new XmlSerializer(typeof(T), xRoot);
return (T)serializer.Deserialize(new StringReader(xml));
}
我得到空對象,因爲實際數據在root-> data-> item中。
我試圖改變xRoot.ElementName = "root";
到xRoot.ElementName = "root/data";
或xRoot.ElementName = "root/data/item";
但我有:
的InnerException信息:System.InvalidOperationException 的HResult = -2146233079 消息=沒有料到。 源= Microsoft.GeneratedCode
對象類是:
public partial class DocumentStatus
{
public DocumentStatus()
{
this.Documents = new HashSet<Document>();
this.DocumentsTrackings = new HashSet<DocumentsTracking>();
this.DocumentsTrackingChildDocuments = new HashSet<DocumentsTrackingChildDocument>();
}
[XmlElement("StateId")]
public int StateId { get; set; }
[XmlElement("StateName")]
public string StateName { get; set; }
[XmlElement("GroupId")]
public Nullable<int> GroupId { get; set; }
public virtual HashSet<Document> Documents { get; set; }
public virtual HashSet<DocumentsTracking> DocumentsTrackings { get; set; }
public virtual HashSet<DocumentsTrackingChildDocument> DocumentsTrackingChildDocuments { get; set; }
}
什麼不清楚'「數據'第1行的開始標記位置73與'root'的結束標記不匹配。 1號線,位置167 – Eser