Reputation: 2580
I'm trying to get the value of the XmlElement out of the Translation.
When I debug the code the value is null. I'm trying to get the value of the Translations.Section.Translation
in Translation.cs
. I can't seem to find out what I'm doing wrong and why. Can anyone explain to me what I need to do?
I came this far but I don't know how to fill the value.
<?xml version="1.0" encoding="utf-8"?>
<Translations code="nl" description="Dutch" xmlns="urn:Test.Translations">
<Section name="Module">
<Translation key="SystemConfiguration">Systeem configuratie</Translation>
</Section>
<Section name="Feature">
<Translation key="Feature">Feature</Translation>
<Translation key="Name">Naam</Translation>
<Translation key="IsEnabled">Actief</Translation>
</Section>
</Translations>
[Serializable()]
[XmlRoot(Namespace = "urn:Test.Translations", ElementName = "Translations", DataType = "string", IsNullable = true)]
public class Translations
{
[XmlAttribute("code")]
public string Code { get; set; }
[XmlAttribute("description")]
public string Description { get; set; }
[XmlElement("Section")]
public List<Section> Sections { get; set; }
}
public class Section
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlElement("Translation")]
public List<Translation> Translations { get; set; }
}
public class Translation
{
[XmlAttribute("key")]
public string Key { get; set; }
//TODO Get value (This is null)
[XmlElement("Translation")]
public string Value { get; set; }
}
var serializer = new XmlSerializer(typeof(Translations), "");
using (var reader = new StreamReader(xmlFilePath))
{
var translationFile = (Translations) serializer.Deserialize(reader);
reader.Close();
}
Upvotes: 2
Views: 827
Reputation: 1264
Try using XML2CSharp to generate class than try again and see if you still get null.
Generated code for your XML looks like this: (You can remove unwanted properties)
/*
Licensed under the Apache License, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0
*/
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="Translation", Namespace="urn:Test.Translations")]
public class Translation {
[XmlAttribute(AttributeName="key")]
public string Key { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName="Section", Namespace="urn:Test.Translations")]
public class Section {
[XmlElement(ElementName="Translation", Namespace="urn:Test.Translations")]
public List<Translation> Translation { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
}
[XmlRoot(ElementName="Translations", Namespace="urn:Test.Translations")]
public class Translations {
[XmlElement(ElementName="Section", Namespace="urn:Test.Translations")]
public List<Section> Section { get; set; }
[XmlAttribute(AttributeName="code")]
public string Code { get; set; }
[XmlAttribute(AttributeName="description")]
public string Description { get; set; }
[XmlAttribute(AttributeName="xmlns")]
public string Xmlns { get; set; }
}
}
Upvotes: 1
Reputation: 169330
Use the [XmlText]
attribute:
public class Translation
{
[XmlAttribute("key")]
public string Key { get; set; }
[XmlText]
public string Value { get; set; }
}
Upvotes: 6