views:

1648

answers:

4

Hi, I am having an issue with serializing and object, I can get it to create all the correct outputs except for where i have an Element that needs a value and an attribute. Here is the required output:

<Root>
  <Method>Retrieve</Method>
  <Options>
    <Filter>
      <Times>
        <TimeFrom>2009-06-17</TimeFrom>
      </Times>
      <Document type="word">document name</Document>
    </Filter>
  </Options>
</AdCourierAPI>

I can build all of it but can not find a way to set the Document type attribute, here is a segment of the object class

[XmlRoot("Root"), Serializable]    
public class Root    
{    
    [XmlElement("Method")]    
    public string method="RetrieveApplications";    
    [XmlElement("Options")]    
    public _Options Options;    
}    
public class _Options    
{
    [XmlElement("Filter")]    
    public _Filter Filter;    
}
public class _Filter    
{
    [XmlElement("Times")]    
    public _Times Times;    
    [XmlElement("Documents")]    
    public string Documents;    
}

which gives me:

<Document>document name</Document>

rather than:

<Document type="word">document name</Document>

but I can not find a way to correct this, please advise.

Thanks

+5  A: 

The string class doesn't have a type property, so you can't use it to create the desired output. You should create a Document class instead :

public class Document
{
    [XmlText]
    public string Name;

    [XmlAttribute("type")]
    public string Type;
}

And you should change the Document property to type Document

Thomas Levesque
I concur (was just about to submit the same thing!)
Cory Larson
+3  A: 

It sounds like you need an extra class:

public class Document
{
    [XmlAttribute("type")]
    public string Type { get; set; }
    [XmlText]
    public string Name { get; set; }
}

Where an instance (in the example) would have Type = "word" and Name = "document name"; documents would be a List<Document>.

By the way - public fields are rarely a good idea...

Marc Gravell
+4  A: 

Where do you have the type stored?

Normally you could have something like:

class Document {
    [XmlAttribute("type")]
    public string Type { get; set; }
    [XmlText]
    public string Name { get; set; }
}


public class _Filter    
{
    [XmlElement("Times")]    
    public _Times Times;    
    [XmlElement("Document")]    
    public Document Document;    
}
bruno conde
Thanks Guys, sorted, much appreciated
A: 

What if I am trying to serialize an array and want to attach an attribute to the array? For example, the output I want is:

<ArrayOfThingie version="1.0">
  <Thingie>
    <name>one</name>
  </Thingie>
  <Thingie>
    <name>two</name>
  </Thingie>
</ArrayOfThingie>

This is just a primitive array, so I don't want to define the attribute for the array itself, just in its serialization. Is there a way to inject an attribute into the serialization?

Jerry
Are you answering this question, or asking one of your own? If you're asking, then use the "Ask a Question" button.
John Saunders