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 541497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:20:57+00:00 2026-05-13T10:20:57+00:00

So I am trying to simply decorate a class to serialize it as XML.

  • 0

So I am trying to simply decorate a class to serialize it as XML. Here’s an example of my problem.

[XmlElement("Dest")]
    public XmlNode NewValue { get; set; }

The real problem here is that sometimes in this implementation the XmlNode can be an XmlElement or XmlAttribute. when it’s an element this code works fine, but when it comes through as an attribute the serializer throws the following error:

System.InvalidOperationException: Cannot write a node of type XmlAttribute as an element value. Use XmlAnyAttributeAttribute with an array of XmlNode or XmlAttribute to write the node as an attribute.

I tried the XmlAnyAttribute, but that failed as well. So simply put, how can I serialize an XmlNode?

For the record, I marked the correct answer below. You kinda got to hack it. Here’s roughly what I implemented myself in case anyone else hits this.

    [XmlIgnore()]
    public XmlNode OldValue { get; set; }

    [XmlElement("Dest")]
    public XmlNode SerializedValue
    {
        get
        {
            if (OldValue == null)
            {
                return null;
            }
            if (OldValue.NodeType == XmlNodeType.Attribute)
            {
                XmlDocumentFragment frag = OldValue.OwnerDocument.CreateDocumentFragment();

                XmlElement elem = (frag.OwnerDocument.CreateNode(XmlNodeType.Element, "SerializedAttribute", frag.NamespaceURI) as XmlElement);

                elem.SetAttribute(OldValue.Name, OldValue.Value);

                return elem;
            }
            else
            {
                return OldValue;
            }
        }
        set
        {
            if (value == null)
            {
                OldValue = null;
                return;
            }
            if ((value.Attributes != null) && (value.NodeType == XmlNodeType.Element) && ((value.ChildNodes == null) || (value.ChildNodes.Count == 0)))
            {
                OldValue = value.Attributes[0];
            }
            else
            {
                OldValue = value;
            }
        }
    }
  • 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-13T10:20:58+00:00Added an answer on May 13, 2026 at 10:20 am

    You can try to hack it (I agree its not very nice):

    XmlNode v;
    
    [XmlElement("Dest")]
    public XmlNode NewValue { get{return v as XmlElement;} set{v = value;} }
    
    [XmlAttribute("Dest")]
    public NewValue { get{return v as XmlAttribute;} set{v = value;} }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm simply trying to crop a JPEG image (no scaling) using PHP. Here is
In a WinApp I am simply trying to get the absolute path from a
I am simply trying to create this string $post_string ='<?xml version=1.0 ?> <AmazonEnvelope xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
Issue Seems like the condition gets ignored. Here is my scenario: Source class public
i'm simply trying to get the current date in PHP, but it's getting '07:45:23'
I'm simply trying to get data from two sql server db tables using ado.net
I am simply trying to get hold of the UIImageView that was tapped on,
Simply trying to get a cursor back for the ids that I specify. CREATE
I'm simply trying to merge 2 xml documents (adding nodes from one into the
Trying to simply grab an XML file and spit its contents through an API

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.