Solved

xml serialization in C#

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to dynamically set the form action using jQuery.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now