I would like to de/serialize a XML-Document with type=array as root node.
The given xml structure looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<parties type="array">
<party type="Person">
<id>1</id>
<lastname>Smith</lastname>
<firstname>Peter</firstname>
...
</party>
<party type="Person">
<id>2</id>
<lastname>Smith</lastname>
<firstname>Sarah</firstname>
...
</party>
<parties type="array">
C# Code looks like this:
[XmlRootAttribute("parties", Namespace = "", IsNullable = false)]
public class Parties
{
private ArrayList contacts = new ArrayList();
public Parties()
{
}
[XmlArray("parties"), XmlArrayItem("party", typeof(Person))]
public ArrayList Contacts
{
get { return contacts; }
set { contacts = value; }
}
}
The resulting xml output is this:
<?xml version="1.0" encoding="utf-8"?>
<parties xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<parties>
<party>
<id>0</id>
<lastname>Smith</last-name>
<firstname>Peter</first-name>
</party>
</parties>
</parties>
The problem is that I have 2 -tags now.
How can I specify array type for the root element? Any ideas how to fix it without changing the given xml schema?
Try this:
[XmlElement("party")]
public ArrayList Contacts
{
get { return contacts; }
set { contacts = value; }
}
Related
I am defined a simple class and serialized it:
public class Test
{
public string Name { set; get; }
}
I am serized this simple object,the code like this:
Test test = new Test();
test.Name = "a";
TextWriter writer = new StreamWriter(#"D:\a.xml");
XmlSerializer s = new XmlSerializer(typeof(Test), "");
s.Serialize(writer, test);
writer.Close();
The a.xml result file like this:
<Test xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="">
<Name>a</Name>
</Test>
That's no problem,but now i want my xml node content like this(change the default element name(like: Test) to user define name,whatever the name is(like: job-scheduling-data)):
<job-scheduling-data xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="">
<Name>a</Name>
</job-scheduling-data>
What can i do to make it right? I don't want my class name like "job-scheduling-data".
[XmlRoot(ElementName = "job-scheduling-data")]
public class Test
{
public string Name { set; get; }
}
you can check this msdn page.
I've the following XML which I can deserialise in C#:
<?xml version="1.0" encoding="iso-8859-1" ?>
<ProductList>
<Product>
<MyDesc><![CDATA[DOCTOR WHO - PLANET OF THE DEA]]></MyDesc>
<ActualStock><![CDATA[5]]></ActualStock>
</Product>
</ProductList>
However, if I add a new node eg MetaData, I do not know how to grab that node and all of its children as text:
<?xml version="1.0" encoding="iso-8859-1" ?>
<ProductList>
<Product>
<MyDesc><![CDATA[DOCTOR WHO - PLANET OF THE DEA]]></MyDesc>
<ActualStock><![CDATA[5]]></ActualStock>
**<MetaData>
<Desc>MD-Description - This is my product desc</Desc>
<Image>MD - The main image</Image>
</MetaData>**
</Product>
</ProductList>
ie I want to grab and store everything within MetaData as one string. The trouble is the children of MetaData are unknown.
Is this possible?
I thought the deserialiser would use this to match the MetaData node:
[XmlElement("MetaData")]
public List<MetaData> MetaDataList
{
get
{
return metaDataList;
}
set
{
metaDataList = value;
}
}
and then figure out MetaData's children using:
private List<MetaData> metaDataList = new List<MetaData>();
public class MetaData
{
[XmlAnyElement]
public List<string> Children
{
get;
set;
}
}
As you can see I'm completely stuck...
Pointers would be appreciated.
Thanks.
Sai.
My XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServerConfig loggingEnabled="1">
<Servers id="2" host="mytest">
<Server host="test1.example.com" port="9999" >
<Name id="newname1">
<FirstName id="myfirstname">hello first name</FirstName>
<SecondName id="myfirstname">hello first name</SecondName>
</Name>
</Server>
<Server host="test2.example.com" port="8888" />
</Servers>
<Servers id="1">
<Server host="test3.example.com" port="9899" >
<Name id="newname2">
<FirstName id="myfirstname">hello first name</FirstName>
</Name>
</Server>
<Server host="test4.example.com" port="8988" />
</Servers>
</ServerConfig>
I want to deserialize this xml to my class
Class
public sealed class ServerConfig
{
public sealed class Server
{
[XmlAttribute("host")]
public string Host { get; set; } // gives me host name
[XmlAttribute("port")]
public int Port { get; set; } // gives my prot number
}
[XmlArray]
public List<Server> Servers { get; set; } // gives me all 4 server lsit
[XmlAttribute("loggingEnabled")]
public int LoggingEnabled { get; set; } // gives me attribute detail
public ServerConfig()
{
Servers = null;
LoggingEnabled = 0;
}
}
Problem
My Problem is I don't know how to access the Attributes of the nested Element Name and sub nested element FirstName/SecondName
WIll be really very thankful.
Thanks.
The simplest way is being provided by Microsoft in VS 2012 onwards which is being brilliantly explained in this link.
XML TO C# Class
All you need to do is copy your xml and then paste special to a class as explained in the link.
Happy Coding.
Thanks,
I am creating a webservice in .net :
WebService :
public class ValidateWebService : System.Web.Services.WebService
{
[WebMethod]
public List<string> ListAllArtists1()
{
List<string> all1 = new List<string>();
all1.Add("Puneet");
all1.Add("03/07/1988");
all1.Add("Delhi");
return all1;
}
}
}
and when I browse this webservice and invoke the method, ths will return me a list in xml format like this :
<?xml version="1.0" encoding="utf-8" ?>
- <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<string>Puneet</string>
<string>03/07/1988</string>
<string>Delhi</string>
</ArrayOfString>
My problem is the format of this xml. I want the xml format like this:
<Name>1</Name>
<DOB>03/07/1988</DOB>
<State>Delhi</State>
So that I can easily read the xml by the name of its nodes.
Please help.
If you want xml in such format, then return object instead of collection of strings:
public Artist GetArtist()
{
Artist artist = new Artist();
artist.Name = "Puneet";
artist.DOB = "03/07/1988"; // consider DateTime type
artist.State = "Delhi";
return artist;
}
This will return serialized Artist instance:
<Artist>
<Name>Puneet</Name>
<DOB>03/07/1988</DOB>
<State>Delhi</State>
</Artist>
I've been playing around with the xml serialization for a while and I've hit a problem with serializing the a list collection. I want to serialize a list collection without the upper element wrapping around it. See example below:
Result serialization:
<?xml version="1.0" encoding="utf-8" ?>
<Person>
<Name>John</Name>
<AddressLine>
<string>Line 1</string>
<string>Line 2</string>
<string>Line 3</string>
</AddressLine>
<Telephone>123456789</Telephone>
</Person>
The serialization I want to output is:
<?xml version="1.0" encoding="utf-8" ?>
<Person>
<Name>John</Name>
<AddressLine>Line 1</AddressLine>
<AddressLine>Line 2</AddressLine>
<AddressLine>Line 3</AddressLine>
<Telephone>123456789</Telephone>
</Person>
I have tried setting different the attributes to my class I'm serializaing from but I can't seem to get anywhere with it. If anyone could show me what attributes I need to use to get my xml serialization to look like the ouput xml I want that would be greatly appreciated.
Cheers!
[Serializable]
public class Person
{
public string Name { get; set; }
[XmlElement]
public List<string> AddressLine { get; set; }
}
Produces desired output:
<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>John</Name>
<AddressLine>1</AddressLine>
<AddressLine>2</AddressLine>
<AddressLine>3</AddressLine>
</Person>