views:

130

answers:

3
<FileTransferSettings>
  <UploadPath src="user">C:\uploads</UploadPath>
  <DownloadPath src="app">C:\downloads</DownloadPath>
</FileTransferSettings>

I'd want to deserialize this XML into a FileTransferSettings object with 2 properties - UploadPath and DownloadPath. But I also want to preserve the src attribute for each property in a way that my code can interrogate it.

I think creating an associated UploadPathSrc and DownloadPathSrc property is a bit awkward and cumbersome.

Is there another way to represent this in .NET? To me, the src attribute seems like it should be treated as metadata. Is there a best practice for this?

(For background into why I'm trying to do this - see my previous question).

Thanks.

+3  A: 

You could create a second class, FileTransferPath which had a string value "Path" and an enum value "Source"

class FileTransferSettings
{
   public FileTransferPath UploadPath { get; set; }
   public FileTransferPath DownloadPath { get; set; }
   // ...
}

class FileTransferPath
{
   public string Path { get; set; }
   public FileTransferSource Source { get; set}

   public enum FileTransferSource
   {
     None,
     User,
     Application,
     // ...
   }
}

Then you could use code like

   obj.UploadPath.Path;
   obj.UploadPath.Source;

There might be better names you could choose for the class properties; I don't know that I like the repetition of Path. obj.Upload.Path or something might be nicer.

Note that you wouldn't be able to directly serialize/deserialize this directly to and from the format you have using XmlSerialization; but it does accomplish what you need. (And you can still serialize to XML, you just have to do a little more work)

Daniel LeCheminant
I think I'll end up going w/ this approach. The only issue I see with this is that if a section has 20 settings and they don't all fit into FileTransferPath, I could end up with class explosion. Thank you though, this will work.
vg1890
@vg1890: Well, depending on your needs, you _could_ include some sort of serializable dictionary equivalent (key/value pairs) to store additional information.
Daniel LeCheminant
+3  A: 

To expand on the general idea from Daniel L.

public Class FileTransferPath
{
  public Enum SourceEnum { User, App }

  public SourceEnum Source { get; set; }  //Automatic properties in 3.5 Syntax
  public string FilePath { get; set; }
}

public Class FileTransferSettings
{
  public FileTransferPath UploadPath { get; set; }
  public FileTransferPath DownLoadPath { get; set; }
}
Charles Graham
Don't you have to make the SourceEnum type public?
Daniel LeCheminant
+4  A: 

In order to serialize and deserialize the XML correctly, you need to decorate the class with XML serialization attribute

[XmlRoot("FileTransferSettings")]
public class FileTransferSettings
{
   [XmlElement("DownloadPath")]
   public DownloadPath DownloadPath { get; set; }
   // ...
}

[XmlType("DownloadPath")]
public class DownloadPath
{ 
  [XmlAttribute]
  public string Src; // or use enum etc
  [XmlText]
  public string Text;
}

// the serialized XML looks like
<FileTransferSettings>
   <DownloadPath Src="...">text</DownloadPath>
   ....
</FileTransferSettings>
codemeit
Nice, +1 for the XML serialization
Daniel LeCheminant