I am currently programming a UWP application and I am having an issue deserializing an xml file.
I succedded serializing an object using this code
private async void button_Click(object sender, RoutedEventArgs e)
{
Model.Personne user = new Model.Personne();
{
user.username = textBox.Text;
user.password = p1.Password;
user.email = textbox1.Text;
user.address = textBox2.Text;
var tracker = new Geolocator();
tracker.DesiredAccuracyInMeters = 5;
var position = await tracker.GetGeopositionAsync();
user.Latitude = position.Coordinate.Latitude;
user.Longitude = position.Coordinate.Longitude;
user.doesExist = true;
}
await SaveToXml(user, "users.xml");
this.Frame.Navigate(typeof(find_me));
}
public static async Task SaveToXml<T>(T user, string filename)
{
if (user == null) { return; }
try
{
XmlDocument xmlDocument = new XmlDocument();
XmlSerializer serializer = new XmlSerializer(user.GetType());
StorageFolder folder = ApplicationData.Current.LocalFolder;
// StorageFile file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
Stream stream = await folder.OpenStreamForWriteAsync(filename, CreationCollisionOption.OpenIfExists);
using (MemoryStream str = new MemoryStream())
{
serializer.Serialize(stream, user);
stream.Position = 0;
xmlDocument.Load(stream);
XmlNode node = xmlDocument.CreateElement("Personnes");
xmlDocument.AppendChild(node);
xmlDocument.Save(stream);
stream.Dispose();
}
}
catch (Exception ex)
{
ex.ToString();
}
}
But when I serialize my object into xml it serializes my objects with always the <?xml version="1.0" encoding="utf-8"?>
and xmlns attributes.
I want to delete all the xmlns attributes and keep only the first xml markup. Is it possible to do it? Or should I try serialize in JSON?
<?xml version="1.0" encoding="utf-8"?>
<Personne xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<username>toto</username>
<password>tata</password>
<email>titi</email>
<address>teuteu</address>
<Latitude>49.201083564616972</Latitude>
<Longitude>-0.37977506716400489</Longitude>
<doesExist>true</doesExist>
</Personne>
<?xml version="1.0" encoding="utf-8"?>
<Personne xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<username>aymann</username>
<password>vierge</password>
<email>a.blal@hotmail.fr</email>
<address>97 rue des Borches</address>
<Latitude>49.20236710462931</Latitude>
<Longitude>-0.39321689482623645</Longitude>
<doesExist>true</doesExist>
</Personne><?xml version="1.0" encoding="utf-8"?>
<Personne xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<username>asalfo</username>
<password>lolo</password>
<email>lolo</email>
<address>lolo</address>
<Latitude>49.202317469778862</Latitude>
<Longitude>-0.39308322124621681</Longitude>
<doesExist>true</doesExist>
</Personne><?xml version="1.0" encoding="utf-8"?>
<Personne xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<username>google</username>
<password>google</password>
<email>google</email>
<address>google</address>
<Latitude>49.202210566144032</Latitude>
<Longitude>-0.39300312109158891</Longitude>
<doesExist>true</doesExist>
</Personne><?xml version="1.0" encoding="utf-8"?>
<Personne xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<username>amadou</username>
<password>dsfjsmlgkj</password>
<email>sdklfjsdg</email>
<address>mfsldkgmljg</address>
<Latitude>49.202213601881432</Latitude>
<Longitude>-0.39299270197801961</Longitude>
<doesExist>true</doesExist>
</Personne>
And the issue for deserializing is that my xml file can't be read because of the xml markup <?xml version="1.0" encoding="utf-8"?>
Here is my deserialize code
public static async void reader()
{
string filename = "users.xml";
List<Model.Personne> users;
StorageFolder folder = ApplicationData.Current.LocalFolder;
Stream stream = await folder.OpenStreamForWriteAsync(filename, CreationCollisionOption.OpenIfExists);
using (var reader = new StreamReader(stream))
{
XmlSerializer deserializer = new XmlSerializer(typeof(List<Model.Personne>),
new XmlRootAttribute("Personne"));
users = (List<Model.Personne>)deserializer.Deserialize(reader);
foreach (var user in users)
{
latitude = user.Latitude;
longitude = user.Longitude;
}
}
}