Solved

xml serialization in C#

Posted on 2004-08-18
4
285 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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 …

773 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