Class Fields to XML Element and attribute - c#

I have the following class that I want to serialize to an XML file
public class HDLCParameters
{
[XmlElement("ClientMACAddress")]
public string ClientMACAddress { get; set; }
}
this is serialized like this:
<HDLCParameters>
<ClientMACAddress>0x10</ClientMACAddress>
</HDLCParameters>
but i need it like this:
<HDLCParameters>
<ClientMACAddress value="0x10" />
</HDLCParameters>
Do i need to create a ClientMACAddress class with a "value" field? isn't there a way to set an element and an attribute with its value?
thanks

Create the ClientMACAddress class, give it a value field, and specify that it's an attribute (XmlAttributeAttribute)

Related

Wrap with multiple XML tags without create diferent classes C#

I have the class
public class TestModel
{
public int Number { get; set; }
public string Text { get; set; }
}
which is converted with the following xml
<TestModel>
<Number>2</Number>
<Text>text</Text>
</TestModel>
But i need to wrap this with multiples xml tags
<Wrapper1>
<Wrapper2>
<TestModel>
<Number>2</Number>
<Text>text</Text>
</TestModel>
</Wrapper2>
</Wrapper1>
Is there any attribute to be able to put this type of wrappers? Like
[XElementWrapper("Wrapper1", "Wrapper2")]
public class TestModel
{
..
}
I dont want to create dummy classes only to contains the wrappers or create the xml element by code
Yes, you can make it by implementing IXmlSerializable

.NET How to change class atribute in runtime?

I've got an issue with changing attribute at runtime.
I'm using https://www.filehelpers.net/ to handle csv but I think this problem is similar for any other custom attributes.
There is a class representing entity:
[DelimitedRecord("\t")]
public class FileHelper
{
[FieldNotEmpty]
public string Id = "1";
public string Name = "Product1"
}
This class is used as generic parameter for file reading engine. What I want to do is to change DelimitedRecord("\t") to a runtime value eg. semicolon .
I know that I can use below code to get attributes but only read.
DelimitedRecordAttribute[] attributes = (DelimitedRecordAttribute[]) typeof(FileHelper).GetCustomAttributes(typeof(DelimitedRecordAttribute), false);
Is there option to change attribute value or remove it and add new attribute new value?

Convert a POCO to json using only certain fields

I have an object, Project, that contains many fields, some complex some not. It is an EF class, so I can't edit it to add attributes.
I just want to generate a JSON object containing 2 of the fields (one int (id) and one string (name))
I'd hate to create another ViewModel just for this...
In my viewmodel I have a List<Project>. Is there a way to use HTML helpers to get a JSON representation of only the properties I choose without using attributes?
Here is an example of the Project class:
public class Project
{
public int Id {get; set; } <-- Serialize this
public string Name { get; set; } <-- Serialize this
public Object AnotherObject [ Get; Set; } <-- Ignore this
....
}
I'd like it to become:
[{"id":"27","name":"test1"},{"id":"34","name":"test2"},{"id":"35","name":"test3"}]
The ultimate goal here to is output the json directly to the view as a var so that it can be used in building a JsGrid.
If there is a way to do it with Html helpers, that would be great.
Thanks!
Json.NET has a great built in ignore feature. If you tag the Property you want to exclude with the [JsonIgnore] attribute, the serializer will not serialize that property.
[JsonIgnore]
public bool IsValid { get; set; }

Deserialize XML with optional tags

I have an XML with an optional tag as follows:
<Config>
<CheckForCompleteTransform>true</CheckForCompleteTransform>
<!-- more tags -->
</Config>
And the class-definition:
public class config {
[System.Xml.Serialization.XmlElement("CheckForCompleteTransform")]
public bool? CheckForCompleteTransform { get; set; }
}
This works if I either set the tag to what I provided within my example-XML above or I omit it completely. But what if I provide the tag as <MyTag/>? If this notation is used I want the serializer to set the corresponding property within my class to true, but I awlays get a
System.FormatException: the string literal '' is not valid for type
Boolean
Any ideas on how to achieve this?
Check this here
u can use [XmlElement("CheckForCompleteTransform", IsNullable=true)] CheckForCompleteTransform property in your class
public class config
{
[XmlElement("CheckForCompleteTransform", IsNullable = true)]
public bool? CheckForCompleteTransform { get; set; }
}
and add xsi:nil="true" attribute to CheckForCompleteTransform tag like this
<CheckForCompleteTransform xsi:nil="true" />
I haven't verified this myself, but have you tried adding a
[XmlElement(IsNullable = true)]
attribute to the CheckForCompleteTransform ?
EDIT:
Ok, how about together with
DefaultValueAttribute(true);

How to deserialize with RestSharp when I have an attribute named "value"?

I am using RestSharp to deserialize a XML file where some of the nodes are like this:
<element value="something" />
The elementes with an attribute called 'value' will not deserialize. Any ideas on how to get RestShap to deserialize this?
The object used to deserialize is like:
public class Object
{
public string Value { get; set; }
}
Please note, the XML is returned from a web service so I do not have the option of changing the attribute name to something different.
Okay, I have found a solution. I think this is somewhat an edge case.
I renamed the variable
public string Value {get;set;}
to
public string value {get;set;}
And now it deserializes perfectly. Guess the Uppercase Value is for the value contained in a XML element only.

Categories

Resources