question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to custom root element on custom serialize?

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
BacchusDcommented, Dec 13, 2019

I have two diffent program and need serialize DTO to xml and send to other program in some xml structure.

public class RootClass {
   public string SomeAttributeProperty {get;set;}

   public A ManyToOneReference {get;set;}

   public B OneToManyReference {get;set;}
   
}

public class A {  
    public string Name {get;set;}
}

public class B {  
    public string Name {get;set;}
}

I need xml in format:

<RootClassNodeName SomeAttributeProperty = "SomeAttributeProperty" Name="ManyToOneReference.Name" >
      <B Name = "BName1" />
       ......
      <B Name = "BNameN" />
  </RootClassNodeName>

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

1reaction
issue-label-bot[bot]commented, Dec 13, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found