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>
Related
In my c# code I am trying to use XML serialization instead of xml linq. I am not sure which one is better.
But for now the issue I am having is that my node is coming twice. So this is my class structure
public class Enterprise
{
public Properties properties;
public List<Person> person;
}
This is the xml I get generated
<?xml version="1.0" encoding="iso-8859-1"?>
<enterprise>
<properties lang="nb">
<Comments>Utlasting av LMS-data</Comments>
<Datasource>SIKT</Datasource>
<Target />
<Datetime>onsdag 24. oktober 2018</Datetime>
</properties>
<person>
<Person>
<sourceid>
<Source>AD</Source>
<Id>123</Id>
</sourceid>
<Userid>mohsin</Userid>
</Person>
As you see the Person tag is coming twice. This is how it is set up
Enterprise enterprise = new Enterprise();
enterprise.properties.LanguageCode = "nb";
enterprise.properties.Comments = "Utlasting av LMS-data";
enterprise.properties.Datasource = "SIKT";
enterprise.properties.Target = "";
enterprise.properties.Datetime = DateTime.Now.ToLongDateString();
List<Person> person = new List<Person>();
person.Add(new Person
{
//sourceid = new SourceId
//{
// Id = "123",
// Source = "AD"
//},
Userid = "mohsin"
});
enterprise.person = person;
Does anyone knows the reason?
When you use List or array it will conside as `XmlArrayItem' to overcome use 'XmlElement'
public class Enterprise
{
public Properties properties;
[XmlElement("person")]
public List<Person> person;
}
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.
I'm having issues correctly serializing selected data from a c# winforms listbox into an XML file.
// save xml config
SessionConfig config = new SessionConfig();
foreach(String listitem in mylistbox.SelectedItems)
{
config.myItems = listitem;
}
config.Serialize(config);
and here's the SessionConfig class
public class SessionConfig
{
// mylistbox
public string myItems { get; set; }
public void Serialize(SessionConfig details)
{
XmlSerializer serializer = new XmlSerializer(typeof(SessionConfig));
using (TextWriter writer = new StreamWriter(string.Format("{0}\\config.xml", Application.StartupPath)))
{
serializer.Serialize(writer, details);
}
}
}
This will output an XML file like this:
<?xml version="1.0" encoding="utf-8"?>
<SessionConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<myItems>Item</myItems>
</SessionConfig>
What I'm trying to do is serialize all selected items, not just one item. And I would like to put each item in its own <Item> tag under the parent <myItems> node...like this:
<?xml version="1.0" encoding="utf-8"?>
<SessionConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<myItems>
<Item>Item</Item>
</myItems>
</SessionConfig>
I then want to be able to use the XML file to grammatically set the listbox selected items, but I'm not sure how I should go by looping through the node values.
This is what I have so far to read the values from my config file:
XmlDocument xml = new XmlDocument();
xml.Load(string.Format("{0}\\config.xml", Application.StartupPath));
XmlNode anode = xml.SelectSingleNode("/SessionConfig");
if (anode != null)
{
if (anode["MyItems"] != null)
}
The myItems property in the SessionConfig class needs to be a list. It's currently just a string and you'll only ever get the last value from the listbox since you overwrite the previous value each time at the line config.myItems = listitem;
Using a structure like this should get you the structure you're looking for:
public Item MyItems { get; set; }
[CollectionDataContract(ItemName = "Item")]
public class Item : List<string>{}
Then instead of using config.myItems = listitem; to add each item - you would use config.MyItems.Add(listItem)
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; }
}