I have some data in an XML file. I am trying to deserialize the XML to some classes I have created.
I have been able to deserialize all the attributes of the PointCode and CodeAttributes elements. However, I can't seem to get the TextListValue attribute of the textList element.
The textList element returns a null value.
I am using c# and using System.Xml.Serialization
[XmlRoot("PointCode")]
public class PointCode
{
[XmlAttribute("codeLinework")]
public string codeLinework { get; set; }
[XmlElement("CodeAttributes")]
public List<CodeAttributes> codeAttributes { get; set; }
}
[XmlRoot("CodeAttributes")]
public class CodeAttributes
{
[XmlAttribute("attributeName")]
public string attributeName { get; set; }
[XmlAttribute("attributeType")]
public string attributeType { get; set; }
[XmlAttribute("valueType")]
public string valueType { get; set; }
[XmlAttribute("valueRegion")]
public string valueRegion { get; set; }
[XmlElement("text")]
public Text text { get; set; }
}
[XmlRoot("text")]
public class Text
{
[XmlElement("textChoiceList")]
public TextChoiceList textChoiceList { get; }
}
[XmlRoot("textChoiceList")]
public class TextChoiceList
{
[XmlElement("textList")]
public List<TextList> textList { get; set; }
}
[XmlRoot("textList")]
public class TextList
{
[XmlAttribute("textListValue")]
public string textListValue { get; set; }
}
Extract of the XML file I am deserializing.
<Code codeName="KERB" codeDesc="Kerbs" codeType="Point">
<PointCode codeLinework="open line">
<CodeAttributes attributeName="String" attributeType="Normal" valueType="Integer" valueRegion="None">
<integer />
</CodeAttributes>
<CodeAttributes attributeName="Type" attributeType="Normal" valueType="Text" valueRegion="ChoiceList">
<text>
<textChoiceList>
<textList textListValue="Square Kerb" />
<textList textListValue="Roll Kerb" />
</textChoiceList>
</text>
</CodeAttributes>
The missing setter for textChoiceList property in the Text class that leads to the text was null as the only property in the class is unable to set value.
So adding the missing setter to the property will solve the issue.
[XmlRoot("text")]
public class Text
{
[XmlElement("textChoiceList")]
public TextChoiceList textChoiceList { get; set; }
}
Sample .NET Fiddle
Related
There is a problem, that the object fields are initialized as null.
I've checked a couple of examples, I've set the field annotations, but seems like I did something wrong.
So here is my xml file:
<?xml version="1.0" encoding="UTF-8"?>
<getInvoiceReply>
<invoiceID value="944659502"/>
<invFastener>
<fastenerID value=""/>
<fastenerName value=""/>
<fastenerCount value=""/>
<fastenerProperty>
<propID value=""/>
<propName value=""/>
<propValue value=""/>
</fastenerProperty>
</invFastener>
</getInvoiceReply>
I've created the class hierarcy.
Root class InvoiceReply :
[XmlRoot("getInvoiceReply")]
public class InvoiceReply
{
[XmlAttribute("invoiceID")]
public string InvoiceId { get; set; }
[XmlArray("invFastener")]
public List<InvFastener> InvFastener { get; set; }
}
class InvFastener :
public class InvFastener
{
[XmlAttribute("fastenerID")]
public string FastenerID { get; set; }
[XmlAttribute("fastenerName")]
public string FastenerName { get; set; }
[XmlAttribute("fastenerCount")]
public string FastenerCount { get; set; }
[XmlArray("fastenerProperty")]
public List<FastenerProperty> FastenerProperty { get; set; }
}
class FastenerProperty:
public class FastenerProperty
{
[XmlAttribute("propID")]
public string PropId { get; set; }
[XmlAttribute("propName")]
public string PropName { get; set; }
[XmlAttribute("propValue")]
public string PropValue { get; set; }
}
Test code:
InvoiceReply i = null;
var serializer = new XmlSerializer(typeof(InvoiceReply));
using (var reader = XmlReader.Create("C:\\filePathHere\\test.xml"))
{
i = (InvoiceReply)serializer.Deserialize(reader);
}
Could anyone please suggest why is this happens?
You have a few issues with your objects. You are trying to get attributes in place of elements and your arrays are not arrays, they are merely complex elements. Below is a working example that matches your xml schema
class Program
{
static void Main(string[] args)
{
string xml = #"<?xml version=""1.0"" encoding=""UTF-8""?>
<getInvoiceReply>
<invoiceID value=""944659502""/>
<invFastener>
<fastenerID value=""""/>
<fastenerName value=""""/>
<fastenerCount value=""""/>
<fastenerProperty>
<propID value=""""/>
<propName value=""""/>
<propValue value=""""/>
</fastenerProperty>
</invFastener>
</getInvoiceReply>";
var serializer = new XmlSerializer(typeof(InvoiceReply));
var i = (InvoiceReply)serializer.Deserialize(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml)));
Console.ReadKey();
}
}
//Generic class for getting value attribute
public class ValueElement
{
[XmlAttribute("value")]
public string Value { get; set; }
}
[XmlRoot("getInvoiceReply")]
public class InvoiceReply
{
[XmlElement("invoiceID")]
public ValueElement InvoiceId { get; set; } //This is a value element
[XmlElement("invFastener")]
public List<InvFastener> InvFastener { get; set; } //This is an element, not an array
}
public class InvFastener
{
[XmlElement("fastenerID")]
public ValueElement FastenerID { get; set; }//This is a value element
[XmlElement("fastenerName")]
public ValueElement FastenerName { get; set; }//This is a value element
[XmlElement("fastenerCount")]
public ValueElement FastenerCount { get; set; }//This is a value element
[XmlElement("fastenerProperty")]
public List<FastenerProperty> FastenerProperties { get; set; } //This is an element, not an array
}
public class FastenerProperty
{
[XmlElement("propID")]
public ValueElement PropId { get; set; }//This is a value element
[XmlElement("propName")]
public ValueElement PropName { get; set; }//This is a value element
[XmlElement("propValue")]
public ValueElement PropValue { get; set; }//This is a value element
}
I'm not able to deserialize the following secition of an XML with C#
<mainfile>
<portfolio>
<fotos>
<foto> <!CDATA[https://whatever.com/fotos/E/400/photo.JPG]]>
</foto>
</fotos>
</portfolio>
<portfolio>
<fotos>
<foto> <!CDATA[https://whatever.com/fotos/E/400/photo1.JPG]]>
</foto>
</fotos>
</portfolio>
</mainfile>
I think it should be quite straight forward, but when deserializing it always returns an empty list. Here is the code:
[XmlRoot("mainfile")]
public class MainFile
{
public MainFile()
{
porftolios= new List<Portfolio>();
}
[XmlElement("portfolio")]
public List<Portfolio> Portfolios{ get; set; }
}
public class Portfolio
{
....
[XmlElement("fotos")]
public List<Foto> Fotos { get; set; }
}
public class Foto
{
[XmlText]
public string data{ get; set; }
}
Thanks.
EDIT.
From HimBromBeere's solution i've chaged the following code, with a successful result:
public class Portfolio
{
....
[XmlArray("fotos")]
[XmlArrayItem("foto")]
public List<Foto> Fotos { get; set; }
}
public class Foto
{
[XmlText]
public string data{ get; set; }
}
When using XmlElement for list-types the container-list-element within the resulting xml is lost. As you do have that container for your fotos-tag, you should use XmlArrayItem for it:
public class Portfolio
{
[XmlArray("fotos")]
[XmlArrayItem("foto")]
public List<Foto> Fotos { get; set; }
}
If you can change the xml however I would suggest to use a consistent style for your collections, either use the container for your portfolio also, or omit it for your fotos.
I have a xml format in following format mentioned below:-
<JobRunnerPluginStaus PluginName="JobRun">
<JobstepStatus>
<JobStatus StepNumber="1" StepStatus="Done"/>
<JobStatus StepNumber="2" StepStatus="Started" />
</JobstepStatus>
</JobRunnerPluginStaus>
I want to get it converted to following class object using Generics and Reflection.
I want to convert the attributes to simple type(PluginName) and the nested property to a list object(JobstepStatus).
public class JobRunnerPluginStaus
{
public List<JobStatus> JobstepStatus { get; set; }
public string PluginName { get; set; }
}
public class JobStatus
{
public int StepNumber { get; set; }
public string StepStatus { get; set; }
}
I mostly use sites like: https://xmltocsharp.azurewebsites.net/
to do the dirty work for me.
Below is how your class hierarchy would look like:
[XmlRoot(ElementName="JobStatus")]
public class JobStatus {
[XmlAttribute(AttributeName="StepNumber")]
public string StepNumber { get; set; }
[XmlAttribute(AttributeName="StepStatus")]
public string StepStatus { get; set; }
}
[XmlRoot(ElementName="JobstepStatus")]
public class JobstepStatus {
[XmlElement(ElementName="JobStatus")]
public List<JobStatus> JobStatus { get; set; }
}
[XmlRoot(ElementName="JobRunnerPluginStaus")]
public class JobRunnerPluginStaus {
[XmlElement(ElementName="JobstepStatus")]
public JobstepStatus JobstepStatus { get; set; }
[XmlAttribute(AttributeName="PluginName")]
public string PluginName { get; set; }
}
I have a problem deserializing XElement.
Here is the xml code:
<phrase level="1">Where are <subject>you</subject> going?</phrase>
and this is the code for the class
[XmlRoot("phrase")]
public class Phrase
{
[XmlAttribute("level")]
public int Level { get; set; }
[XmlElement("subject")]
public string Subject { get; set; }
}
It's possible to get the text value of phrase TAG ("Where are going?") in deserialization?
I found my solution. It was easy, sorry!
My new class:
[XmlRoot("phrase")]
public class Phrase
{
[XmlAttribute("level")]
public int Level { get; set; }
[XmlElement("subject")]
public string Subject { get; set; }
[XmlText]
public string Value { get; set; }
}
...in Value field I can get XMLTAG text value!
I have an object to deserialize but the object has custom type ApplicationLanguage that cannot be serialize.
[Serializable]
public class FieldTranslation
{
// how is this possible?
// does the 'en-us' in this member is reachable in this concept?
//public ApplicationLanguage Lang = ApplicationLanguagesList.Get("en-us");
//public ApplicationLanguage Lang { get; set; }
[XmlAttribute("name")]
public string Name{ get; set; }
public string Tooltip { get; set; }
public string Label { get; set; }
public string Error { get; set; }
public string Language { get; set; }
}
I built an API to get type ApplicationLanguage from the cache like that:
ApplicationLanguage en= ApplicationLanguagesList.Get("en-us");
Is there anyway that I can combine the custom type in the serialization above?
this is the xml:
<Fields lang="en-us">
<Item name="FirstName">
<Tooltip>Please provide your {0}</Tooltip>
<Error>{0} Is not valid</Error>
<Label>First Name</Label>
</Item>
</Fields>
you could change your class struct like
[Serializable]
[XmlRoot("Fields")]
public class FieldCollection
{
[XmlAttribute("lang")]
public string Lanuage { get; set; }
[XmlElement("Item")]
public FieldTranslation[] Fields { get; set; }
}
[Serializable]
public class FieldTranslation
{
[XmlAttribute("name")]
public string Name { get; set; }
public string Tooltip { get; set; }
public string Label { get; set; }
public string Error { get; set; }
public string Language { get; set; }
}
and then set the language property to serialize
Clarification: this answer is based on the pre-edit xml, where there was an
<Language>he</Language>
element. This answer does not apply to the <Fields lang="en-us"> scenario.
Something like:
[XmlIgnore]
public ApplicationLanguage Lang { get; set; }
[XmlElement("Language")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public string LangSerialized {
get { return Lang == null ? null : Lang.Name; } // or where-ever "he" comes from
set { Lang = value == null ? null : ApplicationLanguagesList.GetByName(value); }
}
Here the LangSerialized member is used as a proxy to the short-form of Lang. With XmlSerializer it is required that this member is public, but I've added a few other attributes to make it disappear from most other common usages.
You can manually fill a field in the collection above like this:
[XmlAttribute("lang")]
public string ManuallyFilledLang{ get; set; }
You also could implement a IXmlSerializable interface in your ApplicationLanguage class