How to custom root element on custom serialize?
See original GitHub issueI have some class and need write his property on attribute. But custom serializer create root element without customize. And I can’t rename root node name!
I need use POCO i can’t use XmlRootAttribute. For example:
public class MyClass {
public string SomeAttributeProperty {get;set;}
public object SomeOtherProperty {get;set;}
}
public class MyClassSerializer : IExtendedXmlCustomSerializer<MyClass > {
public TestClass Deserialize(XElement element) {
//now i have root node
//but it is not my custom node!!!
var somePropertyValue = xElement.Attribute("SomeProperty")?.Value;
return new MyClass { SomeProperty = somePropertyValue };
}
public void Serializer(XmlWriter xmlWriter, MyClass obj) {
var rootElement= new XElement("MyClassNodeName");
var someElement = xElement.SetAttributeValue("SomeOtherProperty ");
someElement.Value = obj.SomeOtherProperty ;
rootElement.Add(someElement);
rootElement.WriteTo(xmlWriter);
}
}
On use ExtendedXmlSerializer serialize with MyClassSerializer will be created this xml :
<MyClass xmlns="clr-namespace:Test;assembly=Test">
<MyClassNodeName SomeProperty = "SomePropertyValue">
<SomeOtherProperty> SomeOtherPropertyValue </SomeOtherProperty>
</MyClassNodeName>
</MyClass>
But I need MyClassNodeName as root node!
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (7 by maintainers)
Top Results From Across the Web
XMLSerialization - Custom root attributes
I am working on a project to serialize XML and set it to a web service. The root element is highly customized, and...
Read more >Custom XML Serialization, how to write custom root element?
You can use either IXmlSerializable or use the XML attributes. I use XmlSerializer passing the root in the constructor.
Read more >Jackson JSON - Using @JsonRootName to customize ...
@JsonRootName annotation is used to indicate name of the POJO that should be serialized. For serialization to work with @JsonRootName ...
Read more >Custom XML Serialization, how to write custom root element?
You can use either IXmlSerializable or use the XML attributes. I use XmlSerializer passing the root in the constructor. var MemoryStream ms; var...
Read more >DataContractJsonSerializer Constructor (System.Runtime. ...
Initializes a new instance of the DataContractJsonSerializer class to serialize or deserialize an object of a specified type using the XML root element...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have two diffent program and need serialize DTO to xml and send to other program in some xml structure.
I need xml in format:
XmlSerializer can’t serialize A.Name to attribute. So i find ExtendedXmlSerializer.
If I set configuration via fluent API a can’t configure attribute. When I use UseAutoFormatting() then i can’t custom some other node When I use IExtendedXmlCustomSerializer I can’t custom root node. When I use ISerializer<T> i can’t use .Net parser such as XmlWriter or XDocument.
P.S. xml shema is more difficult and I show piece with problem
Issue-Label Bot is automatically applying the label
question
to this issue, with a confidence of 0.92. Please mark this comment with 👍 or 👎 to give our bot feedback!Links: app homepage, dashboard and code for this bot.