Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6654643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:24:51+00:00 2026-05-26T01:24:51+00:00

I have to XML (de)serialize the following class: this gives the following output: <ArrayOfPropertyFilter

  • 0

I have to XML (de)serialize the following class:

enter image description here

this gives the following output:

<ArrayOfPropertyFilter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                       xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <PropertyFilter>
    <AndOr>And</AndOr>
    <LeftBracket>None</LeftBracket>
    <Property>17</Property>
    <Operator>Equal</Operator>
    <Value xsi:type="xsd:string">lll</Value>
    <RightBracket>None</RightBracket>
  </PropertyFilter>
</ArrayOfPropertyFilter>

and, after deserialization it gives
enter image description here

How can I “tell” to Serializer to keep the Value “as is”, without any XML node….(in the concrete case the Value should be “lll” and not XMLNode containing Text “lll”) ?

EDIT

Bellow is a full working sample in C#. The output is

Value is = ‘System.Xml.XmlNode[]’

using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace WindowsFormsApplication13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PropertyFilter filter = new PropertyFilter();
            filter.AndOr = "Jora";
            filter.Value = "haha";
            filter.Property = 15;

            var xml = filter.SerializeToString();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);

            PropertyFilter cloneFilter = xmlDoc.Deserialize<PropertyFilter>();

            Console.WriteLine("Value is = '{0}'", cloneFilter.Value);
        }
    }

    public class PropertyFilter
    {
        public string AndOr { get; set; }
        public string LeftBracket { get; set; }
        public int Property { get; set; }
        public string Operator { get; set; }
        public object Value { get; set; }
        public string RightBracket { get; set; }
    }

    public static class Utils
    {
        public static string SerializeToString(this object instance)
        {
            if (instance == null)
                throw new ArgumentNullException("instance");
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            XmlSerializer serializer = new XmlSerializer(
                instance.GetType(), null, new Type[0], null, null);
            serializer.Serialize(sw, instance);
            return sb.ToString();
        }

        public static T Deserialize<T>(this XmlDocument xmlDoc)
        {
            XmlNodeReader reader = new XmlNodeReader(xmlDoc.DocumentElement);
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            object obj = serializer.Deserialize(reader);
            T myT = (T)obj;
            return myT;
        }

    }
}

EDIT 2

To stress the Anton answer, the second example (updated with the Groo’s remarks):

using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace WindowsFormsApplication13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PropertyFilter filter = new PropertyFilter();
            filter.AndOr = "Jora";
            var obj = new Hehe();
            obj.Behehe = 4526;
            filter.Value = obj;
            filter.Property = 15;

            var xml = filter.SerializeToString();
            PropertyFilter cloneFilter = xml.Deserialize<PropertyFilter>();

            Console.WriteLine("Value is = '{0}'", cloneFilter.Value);
        }
    }

    public class Hehe
    {
        public int Behehe { get; set; }
        public override string ToString()
        {
            return string.Format("behehe is '{0}'", Behehe);
        }
    }

    public class PropertyFilter
    {
        public string AndOr { get; set; }
        public string LeftBracket { get; set; }
        public int Property { get; set; }
        public string Operator { get; set; }
        //[XmlElement(typeof(Hehe))]
        public object Value { get; set; }
        public string RightBracket { get; set; }
    }

    public static class Utils
    {
        public static string SerializeToString(this object instance)
        {
            if (instance == null)
                throw new ArgumentNullException("instance");
            StringBuilder sb = new StringBuilder();

            XmlSerializer serializer = new XmlSerializer(instance.GetType(), null, new Type[0], null, null);
            using (StringWriter sw = new StringWriter(sb))
            {
                serializer.Serialize(sw, instance);
            }
            return sb.ToString();
        }

        public static T Deserialize<T>(this string xmlString)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (StringReader sr = new StringReader(xmlString))
            {
                return (T)serializer.Deserialize(sr);
            }
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:24:52+00:00Added an answer on May 26, 2026 at 1:24 am

    Gree’s solution will work as long as Value takes values of primitive, XSD-defined types like string and int, or user-defined types mentioned somewhere in T‘s definition (T itself, the types of its properties etc.) As soon as you need to deserialize a value of a type different from these, you must declare all possible types of Value with XmlElementAttribute, e.g.

    [XmlElement (typeof (string))]
    [XmlElement (typeof (int))]
    [XmlElement (typeof (MyType), Namespace = "http://example.com/schemas/my")]
    public object Value { get ; set ; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class that I want to serialize to an XML file
I built the following code to output XML: public static XDocument Serialize<T>(this T source)
I have a class, SerializableColor, to allow me to XML serialize Colors. public class
I have a business class which I need to serialize to xml. It has
I have an xml file.i need to serialize to c# class. <Configs> <Services> <Path>
I have the following xml I'd like to deserialize into a class <?xml version=1.0
I have the following class: import org.apache.commons.beanutils.BeanUtils; import com.thoughtworks.xstream.XStream; ... public class MyBean {
My Java application needs to serialize/deserialize an XML structure received via HTTP. This XML
How can I XML-serialize the following property: public class SomeCollection { [Xml???] public SomeClass
I'm trying to figure out how to serialize the following class into XML (in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.