Solved

xml serialization in C#

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.php tree directory? 5 56
C# Offline Apllication 5 55
Angular - break the dataset in chunks? 7 39
SQL Login 17 40
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

920 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

14 Experts available now in Live!

Get 1:1 Help Now