XML deserialize og Encoding
Hej Eksperter,jeg har et problem når jeg prøver at deserializere noget XML som indeholder æ,ø og å har prøvet lidt frem og tilbage og kan ikke få det til at spille.
Min kode ser således ud:
public List<Client> getClientsFromXml(StringBuilder xml)
{
XmlSerializer xs = new XmlSerializer(typeof (List<Client>));
XmlDocument document = new XmlDocument();
TextReader tx = new StringReader(xml.ToString());
XmlReader reader = XmlReader.Create(tx);
document.Load(reader);
XmlNodeList nodelist = document.SelectNodes("howxml/ArrayOfClient");
XmlDocument doc = new XmlDocument();
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0","utf-8", null);
doc.AppendChild(declaration);
foreach (XmlNode x in nodelist)
{
XmlNode x1 =doc.ImportNode(x, true);
doc.AppendChild(x1);
}
XmlReader xr = XmlReader.Create(new StringReader(doc.OuterXml));
XmlReader x2 = new XmlTextReader(@"c:\test.xml");
List<Client> l = (List<Client>)xs.Deserialize(xr);
return new List<Client>();
}
Når det virker får jeg minXML fra memory men pt. læser jeg fra denne xml fil (C:\test.xml)
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfClient>
<Client>
<SSN>221261-2354</SSN>
<City>Holte</City>
<Zipcode>2840</Zipcode>
<Name></Name>
<Phone />
<Email />
<Notes>
The client will be picked up from her home
by Falck at 12:15
</Notes>
<Street>Søllerødvej 35 4</Street>
</Client>
</ArrayOfClient>
Hvis jeg fjerner ø'er i Søllerød vej så virker det. Hvad skal jeg gøre har prøvet at ændre "utf-8" til "utf-16" men dette kan den ikke forstå.
Mit Client object ser således ud:
public class Client
{
private String name;
private String ssn;
private String phone;
private String street;
private String municipality;
private String city;
private String zipcode;
private String email;
private String password;
private DateTime terminated;
private String notes;
public Client() { }
public String Name
{
get { return name; }
set { name = value; }
}
public String SSN
{
get { return ssn; }
set { ssn = value; }
}
public String Phone
{
get { return phone; }
set { phone = value; }
}
public String Street
{
get { return street; }
set { street = value; }
}
public String Municipality
{
get { return municipality; }
set { municipality = value; }
}
public String City
{
get { return city; }
set { city = value; }
}
public String Zipcode
{
get { return zipcode; }
set { zipcode = value; }
}
public String Email
{
get { return email; }
set { email = value; }
}
public String Password
{
get { return password; }
set { password = value; }
}
public DateTime Terminated
{
get { return terminated; }
set { terminated = value; }
}
public String Notes
{
get { return notes; }
set { notes = value; }
}
private String toString()
{
return this.ssn + this.name;
}
}