Solved

xml serialization in C#

Posted on 2004-08-18
4
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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