Solved

xml serialization in C#

Posted on 2004-08-18
4
286 Views
Last Modified: 2013-11-19
I have created an object that I can serialize in a MemoryStream as an XML. The problem is that serialization does not work as I like. More exactly I want to serialize the object in something like this:

<tag_name tag_attribute="attribute_value">tag_value</tag_name>

but the only thing I could get is:

<tag_name tag_attribute="attribute_value">
    <sub_tag>tag_value</sub_tag>
</tag_name>

I have tried using different XML attribute tags, but no success.
Please provide my a solution
0
Comment
Question by:lehene
  • 2
4 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11830797
Can you post some code?
0
 
LVL 7

Accepted Solution

by:
NipNFriar_Tuck earned 500 total points
ID: 11831026
If you are trying to add the "tag_value" as a node that will cause the issue you are having.  On each node you can add text which would give you what you want.

For instance this code:

      [System.Xml.Serialization.XmlTypeAttribute(TypeName="State")]
      public class DocArbitraryInfo : object, IDisposable {
            // fields n stuff go here...

            [System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
            public string Name {
                  get {
                        return name;
                  }
                  set {
                        name = value;
                  }
            }

            [System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
            public int Key {
                  get {
                        return key;
                  }
                  set {
                        key = value;
                  }
            }
            [XmlTextAttribute(typeof(string))]
            public string Value {
                  get {
                        return val;
                  }
                  set {
                        val = value;
                  }
            }

            // Methods n stuff...
      }

Produces a tag that look like:

<state name="value of name" key=#>
      The text of the value goes here....
</state>

hope this helps...
0
 

Author Comment

by:lehene
ID: 11831102
Sure. The class would be something like this:

public class Trend{
    private TrendElement make;

    [XmlElement (ElementName = "make")]
    public TrendElement Make
    {
        get { return make; }
        set { make = value; }
    }

...

}


public class TrendElement{
    [XmlAttribute (AttributeName = "id")]
    public string id;

    [XmlElement (ElementName = "description") ]
    public string description;

    /// <summary>
    /// Default public contructor - need for the serialization
    /// </summary>
    public TrendElement() { }

    public TrendElement(string id, string description)
    {
        this.id = id;
        this.description = description;
    }
}


and the result is:

<make id="id">
    <description>value</description>
</make>

but I want:
<make id="id">value</make>

With the existing classes structure maybe is not possible to realize the above structure, but I am generally interested if I can serialize in the way that i asked.

0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11831164
What happens if you change:

 [XmlElement (ElementName = "description") ]

to

 [XmlText ]
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question