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

  • Home
  • SEARCH
  • 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 7045403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:31:35+00:00 2026-05-28T02:31:35+00:00

Assuming an XML like this: <my:Root xmlns:my=http://foo/bar> <my:FieldBasic>content</my:FieldBasic> <my:FieldComplex> <html xml:space=preserve xmlns=http://www.w3.org/1999/xhtml> <div><h1>content</h1></div> </html>

  • 0

Assuming an XML like this:

<my:Root xmlns:my="http://foo/bar">
    <my:FieldBasic>content</my:FieldBasic>
    <my:FieldComplex>
        <html xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
            <div><h1>content</h1></div>
        </html>
    </my:FieldComplex>
<my:Root>

and a class like:

[Serializable]
[XmlType(AnonymousType = true, Namespace = "http://foo/bar")]
[XmlRoot(ElementName = "Root", Namespace = "http://foo/bar", IsNullable = false)]
public class MyRoot
{
    public string FieldBasic { get; set; }
    public string FieldComplex { get; set; }
}

How do I deserialize <my:FieldComplex> to a string within FieldComplex? It fails when it finds the HTML inside. I want to make it give me a string with this content:

<html xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
    <div><h1>content</h1></div>
</html>

If I declare FieldComplex as public object FieldComplex (i.e. xsd:anyType) it kinda works and I get a XMLNode[] inside which I can use.

But I need the FieldComplex to be of type string for the serialization as for serialization the XML will not contain HTML, it will be like:

<my:Root xmlns:my="http://foo/bar">
    <my:FieldBasic>content</my:FieldBasic>
    <my:FieldComplex>content</my:FieldComplex>
<my:Root>

Declaring FieldComplex as object will insert these attributes on the <my:FieldComplex> element:

 xmlns:q1="http://www.w3.org/2001/XMLSchema" p3:type="q1:string" xmlns:p3="http://www.w3.org/2001/XMLSchema-instance

and I don’t want that. I also don’t want to use different classes for serialization and deserialization.

So, is it possible?

To make a long story short, is it possible to have this class:

public class MyRoot
{
    public string FieldBasic { get; set; }
    public string FielComplex { get; set; }
}

Serialize to this:

<my:Root xmlns:my="http://foo/bar">
    <my:FieldBasic>content</my:FieldBasic>
    <my:FieldComplex>content</my:FieldComplex>
<my:Root>

and deserialize from this:

<my:Root xmlns:my="http://foo/bar">
    <my:FieldBasic>content</my:FieldBasic>
    <my:FieldComplex>
        <html xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
            <div><h1>content</h1></div>
        </html>
    </my:FieldComplex>
<my:Root>

?

P.S. Just to explain “the why?”. I have a class witch gets serialized. The serialized XML then travels through multiple nodes in the application and eventually comes back but altered like above. The layers do some XML validation and having extra attributes or elements on input fail the validation and stops the flow. I want to map the return XML to the same class. The content is just strings from it’s point of view but of course not the same for serialization/deserialization 🙁

  • 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-28T02:31:36+00:00Added an answer on May 28, 2026 at 2:31 am

    This isn’t quite finished because I can’t remember if you can / how to add the namespace prefix to the root element in Xml Serialization. But if you implement the IXmlSerializable interface in your MyRoot class like this:

    [XmlRoot("Root", Namespace="http://foo/bar")]
    public class MyRoot : IXmlSerializable
    

    Then write the XML serialization methods yourself, something like this:

            void IXmlSerializable.ReadXml(System.Xml.XmlReader reader)
            {
                reader.MoveToContent();
                var outerXml = reader.ReadOuterXml();
                XElement root = XElement.Parse(outerXml);
    
                this.FieldBasic = root.Elements(XName.Get("FieldBasic", "http://foo/bar")).First().Value;
                this.FieldComplex = root.Elements(XName.Get("FieldComplex", "http://foo/bar")).First().Elements().First().Value.Trim();
            }
    
    
    
            void IXmlSerializable.WriteXml(System.Xml.XmlWriter writer)
            {
                writer.WriteRaw(String.Format("\r\n\t<my:FieldBasic>\r\n\t\t{0}\r\n\t</my:FieldBasic>", this.FieldBasic));
                writer.WriteRaw(String.Format("\r\n\t<my:FieldComplex>\r\n\t\t{0}\r\n\t</my:FieldComplex>\r\n", this.FieldComplex));
            }
    

    (Return null from the GetSchema method)

    This should get you at least pretty close to what you’re after.

    You may also find these links helpful.

    IXmlSerializable

    Namespaces

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document that looks like this: <kmsg xmlns=http://url1 xmlns:env=url1 xmlns:xsi=http://www.w3.org/2001/XMLSchemainstance xsi:schemaLocation=http://location
I have an XML element that looks something like this: <content locale=en> </content> The
Assuming you have the following XML: <?xml version=1.0 encoding=utf-8?> <content> <info> <media> <image> <info>
Assuming I have a XML like so: <a> <b> <i>data</i> <ii>data</ii> <iii>data</iii> </b> <b>
Alright I have an xml document that looks something like this: <xml> <list> <partner>
Here is my problem. I have a menu organized like this: <menutree> <menuitem name=Foo>
I have an xml in which i have stored some html under comments like
Assuming I have the following: [Serializable] public class Foo { public Bar bar {
Assuming an XML file with unknown structure (i.e., unknown element and attribute names), like
I have an xml file like this: <uploads> <upload> <name>asd</name> <type>123</name> </upload> <upload> <name>qwe</name>

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.