Object <-> XML serialization

T

Torsten Z

Hi,

how can I create an object to get the following XML by serialization
<ServiceHost>

<HostId>cdf87f74-d15c-44a1-9c77-c03c2c5c1588</HostId>


<ScreenIds>

<ScreenId>f8b0ccba-4c6e-4f05-b1f3-f82c8107e70f</ScreenId>

<ScreenId>5d136051-1bb5-4ce8-bb7c-d1369647cd0e</ScreenId>

</ScreenIds>

</ServiceHost>


I have the following code and get <string> instead of <ScreenId>. Is there
any posibility to get <ScreenId>?

public class ServiceHost

{

private string hostId;

private ScreenIDs screenIds;

public ServiceHost()

{

}

public string HostId

{

get

{

return this.hostId;

}

set

{

this.hostId = value;

}

}

public ScreenIDs ScreenIds

{

get

{

return this.screenIds;

}

set

{

this.screenIds = value;

}

}

}

public class ScreenIDs : System.Collections.CollectionBase

{

public ScreenIDs()

: base()

{

}

#region Public Properties

public string this[int index]

{

get

{

return (string)(this.InnerList[index]);

}

set

{

this.InnerList[index] = value;

}

}

#endregion

#region Public Methods

public void Add(string item)

{

this.InnerList.Add(item);

}

public void Remove(string item)

{

this.InnerList.Remove(item);

}

public void Change(string oldItem, string newItem)

{

this.InnerList.Remove(oldItem);

this.InnerList.Add(newItem);

}

#endregion

}
 
T

Torsten Z

I think that is not the point. The serianilzation is is fully functional,
but I receive:

....
<ScreenIds>
<string>f8b0ccba-4c6e-4f05-b1f3-f82c8107e70f</string>
<string>5d136051-1bb5-4ce8-bb7c-d1369647cd0e</string>
</ScreenIds>
....
because of the DataType string in the collection members. I need something
like new DataType with the name "ScreenId" and DataType "string"

Roman Wagner said:
Seems you have to implement IXmlSerializable in ScreenIds class.

Torsten said:
Hi,

how can I create an object to get the following XML by serialization
<ServiceHost>

<HostId>cdf87f74-d15c-44a1-9c77-c03c2c5c1588</HostId>


<ScreenIds>

<ScreenId>f8b0ccba-4c6e-4f05-b1f3-f82c8107e70f</ScreenId>

<ScreenId>5d136051-1bb5-4ce8-bb7c-d1369647cd0e</ScreenId>

</ScreenIds>

</ServiceHost>


I have the following code and get <string> instead of <ScreenId>. Is there
any posibility to get <ScreenId>?

public class ServiceHost

{

private string hostId;

private ScreenIDs screenIds;

public ServiceHost()

{

}

public string HostId

{

get

{

return this.hostId;

}

set

{

this.hostId = value;

}

}

public ScreenIDs ScreenIds

{

get

{

return this.screenIds;

}

set

{

this.screenIds = value;

}

}

}

public class ScreenIDs : System.Collections.CollectionBase

{

public ScreenIDs()

: base()

{

}

#region Public Properties

public string this[int index]

{

get

{

return (string)(this.InnerList[index]);

}

set

{

this.InnerList[index] = value;

}

}

#endregion

#region Public Methods

public void Add(string item)

{

this.InnerList.Add(item);

}

public void Remove(string item)

{

this.InnerList.Remove(item);

}

public void Change(string oldItem, string newItem)

{

this.InnerList.Remove(oldItem);

this.InnerList.Add(newItem);

}

#endregion

}
 
M

Marc Gravell

Have you tried [XmlArrayItem], as in the below? (note I don't advocate
public fields - this is a test only).

Marc

using System.IO;
using System.Xml.Serialization;
using System;

[Serializable]
public class XmlTest
{
[XmlArrayItem("ScreenId")]
public string[] ScreenIds;
}

class Program
{
static void Main()
{
XmlTest test = new XmlTest();
test.ScreenIds = new string[] {"123","456","789"};
using(StringWriter writer = new StringWriter()) {
XmlSerializer ser = new XmlSerializer(typeof(XmlTest));
ser.Serialize(writer, test);
Console.WriteLine(writer.ToString());
}
}
}
 
M

Marc Gravell

Have also just tested with your code; works fine:

[XmlArrayItem("ScreenId")]
public ScreenIDs ScreenIds {...}

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top