如何反序列化此XML [英] How do I deserialize this XML
问题描述
我已经尝试了所有我可以反序列化这个XML但是根本不起作用
I've tried everything i could to deserialize this XML but doesn't work at all
<?xml version="1.0" encoding="utf-8"?>
<people>
<root>
<getusers>
<IMAGE>name1.png</IMAGE>
<ID>1</ID>
<SUCCESS>TRUE</SUCCESS>
</getusers>
<getusers>
<IMAGE>name2.png</IMAGE>
<ID>2</ID>
<SUCCESS>FALSE</SUCCESS>
</getusers>
</root>
</people>
任何帮助都会非常感激!
-
我尝试了什么:
Any help would be really appreciated !
-N
What I have tried:
public class getusers
{
public string IMAGE;
public string ID;
public string SUCCESS;
}
string xml = File.ReadAllText ( @"D:\l.xml" );
XmlRootAttribute xRoot = new XmlRootAttribute ();
xRoot.ElementName = "people";
xRoot.IsNullable = true;
var xmlSerializer = new XmlSerializer ( typeof ( getusers [] ) , xRoot );
using (TextReader textReader = new StringReader(xml))
{
res = (getusers []) xmlSerializer.Deserialize(textReader);
}
推荐答案
试试这个
try this
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
public class People
{
[XmlArray("root")]
[XmlArrayItem("getusers")]
public List<getusers> Users { get; set; }
}
public class getusers
{
public string IMAGE;
public string ID;
public string SUCCESS;
}
class TestPointer
{
public static void Main()
{
string xml = File.ReadAllText(@"D:\l.xml");
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "people";
xRoot.IsNullable = true;
var xmlSerializer = new XmlSerializer(typeof(People), xRoot);
using (TextReader textReader = new StringReader(xml))
{
var people = (People)xmlSerializer.Deserialize(textReader);
getusers[] result = people.Users.ToArray();
}
}
}
由于WCF和DataContract以及DataMember出现,我只使用它。此代码将起作用:
Since WCF and DataContract and DataMember came along, I use it exclusively. This code will work:
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace YourNameSpace
{
[DataContract(Namespace="")]
public class People
{
[DataMember]
public List<Person> Persons { set; get; }
public People() { }
}
[DataContract(Namespace="")]
public class Person
{
[DataMember]
public int Id { set; get; }
[DataMember]
public string ImageFilePath { set; get; }
[DataMember]
public bool Success { set; get; }
public Person(int id, string imagefilepath, bool success)
{
Id = id;
ImageFilePath = imagefilepath;
Success = success;
}
}
}
如何使用它来读取包含序列化XML的字符串:
How to use it to read a string containing the serialized XML:
// required references:
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
using System.Text; // enable StringBuilder use
// in some Method or EventHandler
People people1 = new People();
people1.Persons = new List<Person>
{
new Person(1, "image1", true),
new Person(2, "image2", false)
};
// serialize to string Type
StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb))
{
dcs.WriteObject(writer, people1);
}
string people_xml = sb.ToString();
序列化输出将如下所示:
The serialized output will look like this:
<People xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Persons>
<Person>
<ID>1</ID>
<IMAGE>image1</IMAGE>
<SUCCESS>true</SUCCESS>
</Person>
<Person>
<ID>2</ID>
<IMAGE>image2</IMAGE>
<SUCCESS>false</SUCCESS>
</Person>
</Persons>
</People>
假设您在名为'people_xml的字符串变量中有上述XML输出:you ca n在内存中读到这样的内容:
Assume you have the above XML output in a string variable named 'people_xml: you can read that into memory like this:
// in some Method or EventHandler
DataContractSerializer dcs = new DataContractSerializer(typeof(People));
People readPeople;
using (var reader = XmlReader.Create(new StringReader(people_xml)))
{
readPeople = (People) dcs.ReadObject(reader);
}
注意:
1.必需的Class DataContract属性,带有可选的'NameSpace参数[DataContract(NameSpace =) )]设置为空字符串意味着生成的XML不会将当前的NameSpace写入其中:这使得XML在您没有NameSpace信息的上下文中可用。
Notes:
1. the required Class DataContract Attribute with the optional 'NameSpace argument [DataContract(NameSpace = "")] set to an empty string means the resulting XML will not have the current NameSpace written into it: this makes the XML usable in contexts where you do not have that NameSpace information.
检查这个out: XML序列化和反序列化:第2部分 [ ^ ]
Check this out: XML Serialization and Deserialization: Part-2[^]
这篇关于如何反序列化此XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!