I defined 3 classes:
public class PublishedPage
{
public string Action { get; private set; }
public string PageGuid { get; set; }
public List<string> SearchableProperties { get; set; }
public PublishedPage()
{
Action = "Published";
SearchableProperties = new List<string>();
}
}
public class DeletedPage
{
public string Action { get; private set; }
public string PageGuid { get; set; }
public DeletedPage()
{
Action = "Deleted";
}
}
public class MovedPage
{
public string Action { get; private set; }
public string PageGuid { get; set; }
public string OldParentGuid { get; set; }
public string NewParentGuid { get; set; }
public MovedPage()
{
Action = "Moved";
}
}
Somewhere in code I have something like this:
List<PublishedPage> publishedPages = GetPublishedPages();
List<MovedPage> movedPages = GetMovedPages();
List<DeletedPage> deletedPages = GetDeletedPages();
Now I want to create a XML file containing these 3 collections but don't know how.
XML should be like this:
<PublishedPages>
<PublishedPage>
<Action>Published</Action>
<PageGuid>.....</PageGuid>
<SearchableProperties>
<Name>Name 1</Name>
<Name>Name 2</Name>
</SearchablePeoperties>
</PublishedPage>
<PublishedPage>
...
<PublishedPage>
</PublishedPages>
<MovedPages>
...
</MovedPages>
<DeletedPages>
...
</DeletedPages>
Any help would be appreciated.
Thank you!
Even though the XmlSerializer is the easiest one, if you already know the schema you can do it with a bit of linq to xml too:
XElement element =
new XElement("PublishedPages",
(from page in publishedPages
select new XElement("PublishedPage",
new XElement("Action", page.Action),
new XElement("PageGuid",page.PageGuid),
new XElement("SearchableProperties",
(from property in page.SearchableProperties
select new XElement("Name",property)))
)
)
);
Serialization is fairly slow. performance -wise. A similar approach would be something like this:
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmltextWriter = new XmlTextWriter(stringWriter) {Formatting = Formatting.Indented};
// Start document
xmltextWriter.WriteStartDocument();
xmltextWriter.WriteStartElement("ROOT");
foreach (PublishedPage page in publishedPages)
{
//Create a page element
xmltextWriter.WriteStartElement("Page");
xmltextWriter.WriteAttributeString("Action", page.Action);
xmltextWriter.WriteAttributeString("SearchableProperties", page.SearchableProperties);
xmltextWriter.WriteEndElement();
}
// Same for the other lists
// End document
xmltextWriter.WriteEndElement();
xmltextWriter.Flush();
xmltextWriter.Close();
stringWriter.Flush();
Use serialization, a good example is here: http://support.microsoft.com/kb/815813
What you need is XML Serialization as indicated by #Kyndings. But I will give you some code snippets to help:
In order to serialize one object your code should be something similar to
string serializedPublishedPage = Serializer.SerializeObject(PublishedPage, typeof(PublishedPage));
To have all three in the same XML you create a function that generates a list of XMLElements :
private List<XmlElement> functionA ()
{
XmlDocument doc = new XmlDocument();
List<XmlElement> elementList = new List<XmlElement>();
XmlElement element;
string serializedPublishedPage = Serializer.SerializeObject(PublishedPage, typeof(PublishedPage));
string serializedDeletedPage = Serializer.SerializeObject(DeletedPage, typeof(DeletedPage));
string serializedMovedPage = Serializer.SerializeObject(MovedPage, typeof(MovedPage));
doc.LoadXml(serializedDemographic);
element = doc.DocumentElement;
elementList.Add(element);
return elementList;
}
And later use it:
XmlDocument xmlData = new XmlDocument();
XmlElement root = xmlData.CreateElement("WHATEVER");
XmlElement Element;
XmlNode Node;
XmlElement AuxElement;
XmlNode AuxNode;
foreach (XmlElement xmlElement in functionA())
{
AuxNode= doc.ImportNode(xmlElement, true);
AuxElement.AppendChild(node);
}
// Now you have your XML objects in AuxElement
Node = xmlData.ImportNode(AuxElement, true);
root.AppendChild(Node);
// you have your full XML in xmlData in xmlData.InnerXml
Try this, I'm using Xml Serialization :
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Pages pages = new Pages();
pages.PublishedPages.Add(
new PublishedPage()
{
PageGuid = Guid.NewGuid().ToString(),
SearchableProperties = new List<string>()
{
"Foo",
"Bar",
}
});
pages.PublishedPages.Add(
new PublishedPage()
{
PageGuid = Guid.NewGuid().ToString(),
SearchableProperties = new List<string>()
{
"Tic",
"Tac",
"Toe",
}
});
pages.DeletedPages.Add(
new DeletedPage()
{
PageGuid = Guid.NewGuid().ToString(),
});
pages.DeletedPages.Add(
new DeletedPage()
{
PageGuid = Guid.NewGuid().ToString(),
});
pages.MovedPages.Add(
new MovedPage()
{
PageGuid = Guid.NewGuid().ToString(),
OldParentGuid = Guid.NewGuid().ToString(),
NewParentGuid = Guid.NewGuid().ToString(),
});
pages.MovedPages.Add(
new MovedPage()
{
PageGuid = Guid.NewGuid().ToString(),
OldParentGuid = Guid.NewGuid().ToString(),
NewParentGuid = Guid.NewGuid().ToString(),
});
Console.WriteLine(SerializeObject(pages));
}
/// <summary>
/// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
/// </summary>
/// <param name="characters">Unicode Byte Array to be converted to String</param>
/// <returns>String converted from Unicode Byte Array</returns>
private static string UTF8ByteArrayToString(Byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
string constructedString = encoding.GetString(characters);
return (constructedString);
}
/// <summary>
/// Converts the String to UTF8 Byte array and is used in De serialization
/// </summary>
/// <param name="pXmlString"></param>
/// <returns></returns>
private static Byte[] StringToUTF8ByteArray(string pXmlString)
{
UTF8Encoding encoding = new UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(pXmlString);
return byteArray;
}
/// <summary>
/// From http://www.dotnetjohn.com/articles.aspx?articleid=173
/// Method to convert a custom Object to XML string
/// </summary>
/// <param name="pObject">Object that is to be serialized to XML</param>
/// <returns>XML string</returns>
public static string SerializeObject(Object pObject)
{
try
{
string xmlizedString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(pObject.GetType());
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xmlTextWriter.Formatting = Formatting.Indented;
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
xs.Serialize(xmlTextWriter, pObject, ns);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
xmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
return xmlizedString;
}
catch (Exception e)
{
System.Console.WriteLine(e);
return null;
}
}
}
[XmlRoot(ElementName="Pages", Namespace="")]
public class Pages
{
public List<PublishedPage> PublishedPages { get; set; }
public List<MovedPage> MovedPages { get; set; }
public List<DeletedPage> DeletedPages { get; set; }
public Pages()
{
PublishedPages = new List<PublishedPage>();
MovedPages = new List<MovedPage>();
DeletedPages = new List<DeletedPage>();
}
}
public class PublishedPage
{
public string Action { get; set; }
public string PageGuid { get; set; }
public List<string> SearchableProperties { get; set; }
public PublishedPage()
{
Action = "Published";
SearchableProperties = new List<string>();
}
}
public class DeletedPage
{
public string Action { get; set; }
public string PageGuid { get; set; }
public DeletedPage()
{
Action = "Deleted";
}
}
public class MovedPage
{
public string Action { get; set; }
public string PageGuid { get; set; }
public string OldParentGuid { get; set; }
public string NewParentGuid { get; set; }
public MovedPage()
{
Action = "Moved";
}
}
}
Related
I am using XML serialization in my project. It's working good but I am facing two issues.
Formatting of nodes
Remove <string></string> from response
Sample object which I am trying to serialize is :
[XmlRoot("result")]
public class SwRequestListModel
{
public SwRequestListModel()
{
SwRequest = new List<SwRequestShortInfo>();
}
[XmlElement("api_version")]
public string ApiVersion { get; set; }
[XmlElement("sw_request")]
public List<SwRequestShortInfo> SwRequest { get; set; }
}
[XmlRoot(ElementName = "sw_request_short_info")]
public class SwRequestShortInfo
{
[XmlElement(ElementName = "sw_ref_number")]
public string SwRefNumber { get; set; }
[XmlElement(ElementName = "db_no")]
public string DbNo { get; set; }
[XmlElement(ElementName = "engine_manufacturing_no")]
public string EngineManufacturingNo { get; set; }
[XmlElement(ElementName = "engine_ref_type")]
public string EngineRefType { get; set; }
[XmlElement(ElementName = "raised_date")]
public string RaisedDate { get; set; }
[XmlElement(ElementName = "raised_by")]
public string RaisedBy { get; set; }
[XmlElement(ElementName = "status")]
public string Status { get; set; }
[XmlElement(ElementName = "approved_by")]
public string ApprovedBy { get; set; }
[XmlElement(ElementName = "system_type")]
public string SystemType { get; set; }
}
The code which I am using to serialize is :
public static string ToXml(SwRequestListModel obj)
{
XmlSerializer s = new XmlSerializer(obj.GetType());
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = true,
Encoding = Encoding.UTF8,
NewLineOnAttributes = true,
NamespaceHandling = NamespaceHandling.OmitDuplicates,
IndentChars = Environment.NewLine
};
using (var sww = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sww, settings))
{
s.Serialize(writer, obj, ns);
return sww.ToString();
}
}
}
Response is display like :
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"><result> <api_version>1.0</api_version> <sw_request> <sw_ref_number>588</sw_ref_number> <db_no>99899</db_no> <engine_manufacturing_no>MF99899</engine_manufacturing_no> <engine_ref_type>X40B</engine_ref_type> <raised_date>22/06/2021</raised_date> <raised_by>Srusti.Thakkar#test.com</raised_by> <status>Requested</status> <approved_by /> <system_type>test</system_type> </sw_request> <sw_request> <sw_ref_number>589</sw_ref_number> <db_no>88552</db_no> <engine_manufacturing_no>MF99899</engine_manufacturing_no> <engine_ref_type>X40B</engine_ref_type> <raised_date>22/06/2021</raised_date> <raised_by>Srusti.Thakkar#test.com</raised_by> <status>Requested</status> <approved_by /> <system_type>UNIC</system_type> </sw_request> <sw_request> <sw_ref_number>590</sw_ref_number> <db_no>33899</db_no> <engine_manufacturing_no>MF99899</engine_manufacturing_no> <engine_ref_type>X40B</engine_ref_type> <raised_date>22/06/2021</raised_date> <raised_by>Srusti.Thakkar#test.com</raised_by> <status>Requested</status> <approved_by /> <system_type>UNIC</system_type> </sw_request> </result></string>
Actual API Method is :
[HttpGet]
public string GetRequestList(string date, string status, string systemType)
{
SwRequestListModel result = new SwRequestListModel();
result.ApiVersion = "1.0";
//Here get data
return result.ToXml();
}
Browser result is :
Update :
I guess API method GetRequestList serialize string object. Try to return raw string.
[HttpGet]
public HttpResponseMessage GetRequestList(string date, string status, string systemType)
{
SwRequestListModel result = new SwRequestListModel();
result.ApiVersion = "1.0";
//Here get data
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(result.ToXml());
return response;
}
I have the following class and method in one cs file. I have assigned the property values for the class inside the method.
How do I read the values that are assigned to the properties in the ReturnValue class using another another form.
public class ReturnValues
{
public int startdate { get; set; }
public string imageurl { get; set; }
public string headline { get; set; }
public string fulldownloadLink { get; set; }
public string imagecopyright { get; set; }
public string filename { get; set; }
public int PreviousFileDate { get; set; }
}
string prtemp_path = Path.GetTempPath();
public void BingWallpaepr()
{
ReturnValues returnValues = new ReturnValues();
string baseurl = "http://bing.com";
//set the link for XML
var xml_link = "http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-US";
string xmlStr;
using (var wc = new WebClient())
{
xmlStr = wc.DownloadString(xml_link); //Downloads the xml page
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr); //loads the xml page to the string
XmlElement root = xmlDoc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/images/image");
foreach (XmlNode node in nodes)
{
returnValues.imageurl = node["url"].InnerText;
returnValues.headline = Regex.Replace(node["headline"].InnerText, "[^A-Za-z0-9 _]", "");
returnValues.imagecopyright = node["copyright"].InnerText;
returnValues.startdate = int.Parse(node["startdate"].InnerText);
//download the new image
returnValues.filename = string.Format(#"\bing_{0}.jpg", returnValues.startdate);
// MessageBox.Show(returnValues.filename.ToString());
returnValues.fulldownloadLink = baseurl + returnValues.imageurl;
wc.DownloadFile(new Uri(returnValues.fulldownloadLink), prtemp_path + returnValues.filename);
}
In my other form, I am trying to get the property value like below:
namespace CrimePortal
{
public partial class Loginfrm : Form
{
public Loginfrm()
{
InitializeComponent();
PlatformCommands.ReturnValues returnValues = new PlatformCommands.ReturnValues();
MessageBox.Show(returnValues.startdate.ToString());
}
}
but it returns null.
You are declaring BingWallpaper as a void. It shouldn't be. The declaration should look like this:
public ReturnValues BingWallpaepr()
And at the end of the method you should return returnValues;
Then, from your first form (login), you need to have a reference to the instance of the other form, let's say called platformCommands. There are many ways to do this and it depends on how you chose to design and structure your UI.
For example, if you want to instantiate the Platform Commands just before displaying the MessageBox, you could do
var platformCommands = new PlatformCommands();
PlatformCommands.ReturnValues returnValues = platformCommands.BingWallpaper();
MessageBox.Show(returnValues.startdate.ToString());
I have complex class which I want to serialize to XML format using costum attributes. I am stucked at XMLelement which is List and I would like to generate new items with constructor, where I can fill attribute name and text value.
Now I have to create seperate objects and those I can add to the List. I want to simplify this.
class to be serialized:
[XmlElement("Cfg")]
public ElCfg Cfg = new ElCfg();
public class ElCfg
{
[XmlAttribute("Name")] public string CfgName { get; set; } = "Default";
[XmlElement("Content")] public ElCont Content = new ElCont();
}
public class ElCont
{
[XmlAttribute("ver")] public string ContentVer { get; set; }
[XmlElement("Prop")] public List<ElProp> Properties = new List<ElProp>();
}
public class ElProp
{
[XmlAttribute("Name")]
public string PropertyName { get; set; }
[XmlText]
public string PropertyVal { get; set; }
}
usage in main:
static void Main(string[] args)
{
//build promotic object
PromoticXML xmlDoc = new PromoticXML();
xmlDoc.Cfg.Content.ContentVer = "80323";
PromoticXML.ElProp prop1 = new PromoticXML.ElProp();
prop1.PropertyName = "neco";
prop1.PropertyVal = "necojineho";
PromoticXML.ElProp prop2 = new PromoticXML.ElProp();
prop2.PropertyName = "neco";
prop2.PropertyVal = "necojineho";
xmlDoc.Cfg.Content.Properties.Add(prop1);
xmlDoc.Cfg.Content.Properties.Add(prop2);
//serialize promotic object
XmlWriterSettings xmlSet = new XmlWriterSettings();
xmlSet.Encoding = Encoding.Unicode;
xmlSet.Indent = true;
xmlSet.IndentChars = " ";
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
XmlSerializer serializer = new XmlSerializer(typeof(PromoticXML));
using (XmlWriter writer = XmlWriter.Create("promotic.xml", xmlSet))
{
serializer.Serialize(writer, xmlDoc);
}
Process.Start("notepad.exe", "promotic.xml");
}
What my goal is:
xmlDoc.Cfg.Content.Properties.Add(new PromoticXML.ElProp("someName", "someText"));
instead of:
PromoticXML.ElProp prop1 = new PromoticXML.ElProp();
prop1.PropertyName = "neco";
prop1.PropertyVal = "necojineho";
xmlDoc.Cfg.Content.Properties.Add(prop1);
I'm trying to add schemaLocation attribute to XML root element when serializing List<T>. Code works fine if I'm serializing just one object but does not work on lists. My current code:
public class SomeObject
{
public int Id { get; set; }
public string Name { get; set; }
}
public class XmlListContainer<T> : List<T>
{
[XmlAttribute(Namespace = XmlSchema.InstanceNamespace)]
public string schemaLocation = "http :// localhost/someschema";
}
public class BuildXml
{
public static void GetXml()
{
var list = new XmlListContainer<SomeObject>()
{
new SomeObject() { Id = 1, Name = "One" },
new SomeObject() { Id = 2, Name = "Two" },
};
var objectToXml = list;
string output;
using (var writer = new StringWriter())
{
var xs = new XmlSerializer(objectToXml.GetType());
var nameSpaces = new XmlSerializerNamespaces();
nameSpaces.Add("xsi", "http :// www.w3.org/2001/XMLSchema-instance");
xs.Serialize(writer, objectToXml, nameSpaces);
output = writer.GetStringBuilder().ToString();
writer.Close();
}
Console.WriteLine(output);
}
}
XML root element appears without schemaLocation:
<ArrayOfSomeObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
If I'm changing code to
public class SomeObject
{
public int Id { get; set; }
public string Name { get; set; }
[XmlAttribute(Namespace = XmlSchema.InstanceNamespace)]
public string schemaLocation = "http :// localhost/someschema";
}
...
var objectToXml = new SomeObject() { Id = 1, Name = "One" };
...all looks fine but I need list list
<SingleObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost/someschema">
Is it possible to add schemaLocation attribute when serializing List<T>?
Maybe i misunderstood something for serialization. i wanna serialize my .net object fastest way. i made some googling i found protobuf. Myfirstquestion is ProtoBuf.Net has avility for xml serailization.if it has, can i use it for xml serialization.
My model:
[XmlType]
public class CT {
[XmlElement(Order = 1)]
public int Foo { get; set; }
}
[XmlType]
public class TE {
[XmlElement(Order = 1)]
public int Bar { get; set; }
}
[XmlType]
public class TD {
[XmlElement(Order = 1)]
public List CTs { get; set; }
[XmlElement(Order = 2)]
public List TEs { get; set; }
[XmlElement(Order = 3)]
public string Code { get; set; }
[XmlElement(Order = 4)]
public string Message { get; set; }
[XmlElement(Order = 5)]
public DateTime StartDate { get; set; }
[XmlElement(Order = 6)]
public DateTime EndDate { get; set; }
}
my serializer :
CT ct = new CT() { Foo = 1 };
List<CT> list = new List<CT>();
list.Add(ct);
TE te = new TE() { Bar = 2 };
List<TE> list2 = new List<TE>();
list2.Add(te);
TD td = new TD() { Code = "Test",Message = "Yusuf",StartDate = DateTime.Now,EndDate = DateTime.Now,CTs = list,TEs = list2 };
List<TD> list3 = new List<TD>();
list3.Add(td);
Stopwatch stopwatch5 = new Stopwatch();
stopwatch5.Start();
string str = String.Empty;
using (MemoryStream stream = new MemoryStream())
{
byte[] data = Serialize(list3);
XmlDocument doc = new XmlDocument();
string xml = Encoding.UTF8.GetString(data); <--SHOULD CREATE XML
doc.LoadXml(xml);
// str = Convert.ToBase64String(stream.GetBuffer(),0,(int)stream.Length);
}
stopwatch5.Stop();
Console.WriteLine(((double)(stopwatch5.Elapsed.TotalMilliseconds * 1000000) / 1000000).ToString("0.00 ns"));
Console.Read();
}
public static byte[] Serialize(List<TD> tData) {
using(var ms = new MemoryStream()) {
ProtoBuf.Serializer.Serialize(ms,tData);
return ms.ToArray();
}
}
public static List<TD> Deserialize(byte[] tData) {
using(var ms = new MemoryStream(tData)) {
return ProtoBuf.Serializer.Deserialize<List<TD>>(ms);
}
}
it should create xml as a result of " string xml = Encoding.UTF8.GetString(data);". But doesn't. How can i produxe xml result?
Protocol buffers doesn't serialize objects to XML.
It produces binary data. And it has its own set of attributes.
Check this answer
Is Protobuf-net's serialization/deserialization faster than XML ? Yes, by far.
However XmlSerializer is fast enough for most of the tasks.
What you should remind when using it though, is:
XmlSerializer instance is generating code on the fly and compile this code into an assembly.
This generated assembly is then used to serialize and deserialize your objects really fast.
So you should cache instances of XmlSerializer (and avoid recreating them)
you could add a warm up by calling Serialize and Deserialize in order to initialize inner objects and jit them.
You could even go further by generating the auto-generated assembly by yourself, but then you should remember to regenerate each time you change the objects (It can be automated with a MsBuild Task).
You can also look further optimizations:
On Stack Overflow
With Sgen
You can only have one tag at the root level of your xml. So either TD cannot be a list, or you must have an outer tag around the List. This code works
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TD td = new TD()
{
Code = "Test",
Message = "Yusuf",
StartDate = DateTime.Now,
EndDate = DateTime.Now,
CTs = new List<CT>() {
new CT() { Foo = 1},
new CT() { Foo = 2},
new CT() { Foo = 3},
},
TEs = new List<TE>() {
new TE() { Bar = 1},
new TE() { Bar = 2},
new TE() { Bar = 3},
}
};
using (MemoryStream stream = new MemoryStream())
{
byte[] data = Serialize(td);
XmlDocument doc = new XmlDocument();
string xml = Encoding.UTF8.GetString(data);
doc.LoadXml(xml);
// str = Convert.ToBase64String(stream.GetBuffer(),0,(int)stream.Length);
}
}
public static byte[] Serialize(TD tData)
{
using (var ms = new MemoryStream())
{
XmlSerializer serializer = new XmlSerializer(typeof(TD));
serializer.Serialize(ms, tData);
return ms.ToArray();
}
}
public static TD Deserialize(byte[] tData)
{
using (var ms = new MemoryStream(tData))
{
XmlSerializer xs = new XmlSerializer(typeof(TD));
return (TD)xs.Deserialize(ms);
}
}
}
[XmlRoot("CT")]
public class CT
{
[XmlElement(ElementName = "Foo", Order = 1)]
public int Foo { get; set; }
}
[XmlRoot("TE")]
public class TE
{
[XmlElement(ElementName = "Bar", Order = 1)]
public int Bar { get; set; }
}
[XmlRoot("TD")]
public class TD
{
[XmlElement(ElementName = "CTs", Order = 1)]
public List<CT> CTs { get; set; }
[XmlElement(ElementName = "TEs", Order = 2)]
public List<TE> TEs { get; set; }
[XmlElement(ElementName = "Code", Order = 3)]
public string Code { get; set; }
[XmlElement(ElementName = "Message", Order = 4)]
public string Message { get; set; }
[XmlElement(ElementName = "StartDate", Order = 5)]
public DateTime StartDate { get; set; }
[XmlElement(ElementName = "EndDate", Order = 6)]
public DateTime EndDate { get; set; }
}
}