i wanna read this xml and convert it to list for inserting into database
please help
<?xml version="1.0" encoding="UTF-8"?>
-<xml>-<records>-<record><database path="J:\EndNote\PA-03\1.enl" name="1.enl">1.enl</database><source-app name="EndNote" version="14.0">EndNote</source-app><rec-number>38</rec-number>-<foreign-keys><key db-id="r50aeetv1taarsewtwsvrr2h2wtzde5z25pp" app="EN">38</key></foreign-keys><ref-type name="Journal Article">17</ref-type>-<contributors>-<authors>-<author><style size="100%" font="default" face="normal">Patrick Carpenter</style></author></authors></contributors>...
EDIT:
its for Exported EndNote
its so messy and it does not do the trick
please check this
full xml link
Quickest way to do this is to use xsd.exe to generate serialization classes :
1) generate schema definition
xsd file.xml [/outputdir:directory]
This step is not needed, if xml supplier has xsd schemas allready (sample xml does not have defined any used xsd schemas)
2) generate classes from xsd schema:
xsd file.xsd {/classes | /dataset} [/element:element]
[/language:language] [/namespace:namespace]
[/outputdir:directory] [URI:uri]
3) use xml serializer to deserialize from generated classes:
http://msdn.microsoft.com/en-us/library/dsh84875.aspx
Create classes for the model of the XML contents such as:
class Records
{
public List<Record> records { get; set }
}
class Record
{
public RecordDatabase database { get; set }
...
public int rec_number { get; set }
...
}
class RecordDatabase
{
public string path { get; set }
public string name { get; set }
}
...
Then you can deserialize your XML File into an object:
Records records = null;
string path = "importfile.xml";
XmlSerializer serializer = new XmlSerializer(typeof(Cars[]));
StreamReader reader = new StreamReader(path);
reader.ReadToEnd();
records = (records)serializer.Deserialize(reader);
reader.Close();
Related
This is my current XML Response tag
<Response_Data_1234 diffgr:id="Response_Data_1234" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<Status>File received.</Status>
<Time>2022-01-25T09:44:15.73+08:00</Time>
<Location>HKG</Location>
</Response_Data_1234>
the number 1234 in <Response_Data_1234> is a Response Data id, which will be dynamic depending on the request.
Could someone please me create a C# class in this scenario so that I can map the response directly to the class. Thanks in advance
Actually you should not have any unique identifier attached to XML element name. It should be added as attribute.
To answer your question:
There is no direct class attribute to ignore class name while deserialisation of xml string to object.
Option1 (better): Create mapper extension method for XNode to class attribute mapping and reuse it.
Option2:
If your xml string is small you can try to update name of root element and use it to deserialise.
sample code:
var responseDataXDoc = XDocument.Parse(xml);
responseDataXDoc.Root.Name = "ResponseData";
var serializer = new XmlSerializer(typeof(ResponseData));
var responseData = serializer.Deserialize(new StringReader(responseDataXDoc.ToString(SaveOptions.DisableFormatting)));
With Cinchoo ETL - an open source library, you can deserialize such xml easily as below
using (var r = ChoXmlReader<ResponseData>.LoadText(xml)
.WithXPath("//")
.WithXmlNamespace("diffgr", "")
.WithXmlNamespace("msdata", "")
)
{
var rec = r.FirstOrDefault();
rec.Print();
}
public class ResponseData
{
public string Status { get; set; }
public DateTime Time { get; set; }
public string Location { get; set; }
}
Sample fiddle: https://dotnetfiddle.net/HOOPOg
I am new to unit testing and I am wondering what would be the best practices for unit testing xml deserialisation.
Consider the following xml:
<people>
<person id="1">
<name>Joe</name>
<age>28</age>
</person>
<person id="2">
<name>Jack</name>
<age>38</age>
</person>
</people>
And the following model class for the people:
[XmlRoot(ElementName ="people")]
public class People
{
public People() { PeopleList = new List<Person>(); }
[XmlElement("person")]
public List<Person> PeopleList { get; set; }
}
public class Person
{
[XmlAttribute("id")]
public int id { get; set; }
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("age")]
public int Age { get; set; }
}
I deserialize the xml using:
public List<Person> GetListOfPeople()
{
People plist = new People();
string content;
using (StreamReader sr = new StreamReader(manager.Open("People.xml")))
{
var serializer = new XmlSerializer(typeof(People));
plist = (People)serializer.Deserialize(sr);
}
return plist.PeopleList;
}
What would be the best methods to unit test the GetListOfPeople method above?
If you can change your method to take an xml file as an input parameter, you can have a sample xml file created and added to your test project. Since you know the values of your xml file, you can start comparing the values directly.
Considering you'll use the sample file you provided in your test, you can verify like this:
var persons = x.GetListOfPeople("sample.xml");
Assert.AreEqual("Joe", persons[0].Name);
Assert.AreEqual(38, persons[1].Age);
If the xml file is coming to your code from some source and you think it couldn't be following your xml schema all the time, then probably you can create some sample xml files again which violate your schema and prepare tests to call your method which should throw some exception if schema is not correct.
Hope this helps.
I have XML file like this:
<?xml version="1.0"?>
<catalog>
<book id="1" date="2012-02-01">
<title>XML Developer's Guide</title>
<price>44.95</price>
<description>
An in-depth look at creating applications
with XML.
</description>
</book>
<book id="2" date="2013-10-16">
<author>Mark Colsberg</author>
<title>Dolor sit amet</title>
<price>5.95</price>
<description>Lorem ipsum</description>
</book>
</catalog>
How to quick convert it to C# classes to use access data by LINQ?
Do I have to write the class manually for any XML file case?
What about JSON format?
Is the XSD the only solution?
You have two possibilities.
Method 1. XSD tool
Suppose that you have your XML file in this location C:\path\to\xml\file.xml
Open Developer Command Prompt
You can find it in Start Menu > Programs > Microsoft Visual Studio 2012 > Visual Studio Tools
Or if you have Windows 8 can just start typing Developer Command Prompt in Start screen
Change location to your XML file directory by typing cd /D "C:\path\to\xml"
Create XSD file from your xml file by typing xsd file.xml
Create C# classes by typing xsd /c file.xsd
And that's it! You have generated C# classes from xml file in C:\path\to\xml\file.cs
Method 2 - Paste special
Required Visual Studio 2012+
Copy content of your XML file to clipboard
Add to your solution new, empty class file (Shift+Alt+C)
Open that file and in menu click Edit > Paste special > Paste XML As Classes
And that's it!
Usage
Usage is very simple with this helper class:
using System;
using System.IO;
using System.Web.Script.Serialization; // Add reference: System.Web.Extensions
using System.Xml;
using System.Xml.Serialization;
namespace Helpers
{
internal static class ParseHelpers
{
private static JavaScriptSerializer json;
private static JavaScriptSerializer JSON { get { return json ?? (json = new JavaScriptSerializer()); } }
public static Stream ToStream(this string #this)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(#this);
writer.Flush();
stream.Position = 0;
return stream;
}
public static T ParseXML<T>(this string #this) where T : class
{
var reader = XmlReader.Create(#this.Trim().ToStream(), new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Document });
return new XmlSerializer(typeof(T)).Deserialize(reader) as T;
}
public static T ParseJSON<T>(this string #this) where T : class
{
return JSON.Deserialize<T>(#this.Trim());
}
}
}
All you have to do now, is:
public class JSONRoot
{
public catalog catalog { get; set; }
}
// ...
string xml = File.ReadAllText(#"C:\path\to\xml\file.xml");
var catalog1 = xml.ParseXML<catalog>();
string json = File.ReadAllText(#"C:\path\to\json\file.json");
var catalog2 = json.ParseJSON<JSONRoot>();
Here you have some Online XML <--> JSON Converters: Click
You can follow this simple step
1.Please Add using System.Xml as a reference;
2.Make a class named book in this way
public class book
{
public Nullable<System.DateTime> date{ get; set; }
public decimal price { get; set; }
public string title { get; set; }
public string description { get; set; }
}
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Write down full path");
XmlNodeList dataNodes = xmlDoc.SelectNodes("/catalog");
foreach (XmlNode node in dataNodes)
{
book objbook = new book();
objbook.date=Convert.ToDateTime(node.Attributes["date"].Value);
objbook.title=node.SelectSingleNode("title").InnerText;
objbook.description=node.SelectSingleNode("description").InnerText;
objbook.price=Convert.ToDecimal(node.SelectSingleNode("price").InnerText);
}
}
catch(Exception ex)
{
throw ex;
}
Use the XML Schema Definition Tool xsd.exe found in your framework tools to convert your schema into a serializable class or dataset.
xsd file.xsd {/classes | /dataset} [/element:element]
[/language:language] [/namespace:namespace]
[/outputdir:directory] [URI:uri]
And in example, whereas the C# class will be generated in the same directory as the xsd tool:
xsd /c YourFile.xsd
Use the super simple way using 'Paste XML As Classes' functionality in Visual studio menu.
1.copy the xml source in the clipboard, something like CTRL+A and CTRL+C
2.Go to 'Edit' Menu -> Paste Special -> Paste XML As Classes, to paste the generated classes based on the source xml"
Ref: More steps in detail at this link
Here is one another easy way to parse xml file using Cinchoo ETL, an open source library
Define POCO class as below
public class Book
{
[ChoXPath("#id")]
public int Id { get; set; }
[ChoXPath("#date")]
public DateTime Date { get; set; }
[ChoXPath("author")]
public string Author { get; set; }
[ChoXPath("title")]
public string Title { get; set; }
[ChoXPath("price")]
public double Price { get; set; }
[ChoXPath("description")]
public string Description { get; set; }
}
Use it to the parser as below
using (var r = new ChoXmlReader<Book>("*** Xml file path ***")
.WithXPath("//catalog/book", true)
)
{
foreach (var rec in r)
rec.Print();
}
Sample fiddle: https://dotnetfiddle.net/3UI82F
I'm working on a WP7 app which gets and updates data on a web server. If any updates need a response, I get a list of errors that needs to be dealt with, and a list of possible choices for each error. The issue I'm having is assigning each object its appropriate list of choices. As of now I get a list of errors, and another list of all possible choices for all errors. I'd like the error object to contain the list of only its options so I can handle that.
So here's an example response:
<?xml version="1.0" encoding="utf-8"?>
<response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<response_error_dialogs>
<error_dialog_list>
<error_dialog_choice>
<error_dialog_id>1301</error_dialog_id>
<error_dialog_message>You have changed the phone number. Select which phone number to make the primary contact number.</error_dialog_message>
<error_dialog_title>Phone Number Changed</error_dialog_title>
<error_dialog_is_set>false</error_dialog_is_set>
<error_dialog_choice_option_list>
<error_dialog_choice_option>
<error_dialog_choice_option_id>1</error_dialog_choice_option_id>
<error_dialog_choice_option_title>Home</error_dialog_choice_option_title>
</error_dialog_choice_option>
<error_dialog_choice_option>
<error_dialog_choice_option_id>2</error_dialog_choice_option_id>
<error_dialog_choice_option_title>Mobile</error_dialog_choice_option_title>
</error_dialog_choice_option>
<error_dialog_choice_option>
<error_dialog_choice_option_id>3</error_dialog_choice_option_id>
<error_dialog_choice_option_title>Work</error_dialog_choice_option_title>
</error_dialog_choice_option>
</error_dialog_choice_option_list>
</error_dialog_choice>
<error_dialog_choice>
<error_dialog_id>1303</error_dialog_id>
<error_dialog_message>You have changed the account email address. Would you like this to be the new default email?</error_dialog_message>
<error_dialog_title>Email Address Changed</error_dialog_title>
<error_dialog_is_set>false</error_dialog_is_set>
<error_dialog_choice_option_list>
<error_dialog_choice_option>
<error_dialog_choice_option_id>1</error_dialog_choice_option_id>
<error_dialog_choice_option_title>No</error_dialog_choice_option_title>
</error_dialog_choice_option>
<error_dialog_choice_option>
<error_dialog_choice_option_id>2</error_dialog_choice_option_id>
<error_dialog_choice_option_title>Yes</error_dialog_choice_option_title>
</error_dialog_choice_option>
</error_dialog_choice_option_list>
</error_dialog_choice>
</error_dialog_list>
</response_error_dialogs>
</response>
And here's the classes used:
public class ErrorDialog
{
XElement self;
public ErrorDialog() { }
public ErrorDialog(XElement errorDialog)
{
self = errorDialog;
}
public int errorDialogId
{
get { return (int)(self.Element("error_dialog_id")); }
}
public string errorDialogMessage
{
get { return (string)(self.Element("error_dialog_message")); }
}
public string errorDialogTitle
{
get { return (string)(self.Element("error_dialog_title")); }
}
public bool errorDialogIsSet
{
get { return (bool)(self.Element("error_dialog_is_set")); }
}
public List<ErrorDialogChoice> errorDialogChoice
{
get { return (List<ErrorDialogChoice>)(errorDialogChoice); }
}
public int errorDialogSelectedOption
{
get { return (int)(self.Element("error_dialog_selected_option")); }
}
}
class ErrorDialogChoice
{
XElement self;
public ErrorDialogChoice() { }
public ErrorDialogChoice(XElement errorDialogChoice)
{
self = errorDialogChoice;
}
public int errorDialogChoiceOptionId
{
get { return (int)(self.Element("error_dialog_choice_option_id")); }
}
public string errorDialogChoiceOptionTitle
{
get { return (string)(self.Element("error_dialog_choice_option_title")); }
}
}
And here's how I'm parsing it:
XElement response = XElement.Parse(data);
ErrorDialog[] dialogs = response
.Element("response_error_dialogs")
.Element("error_dialog_list")
.Elements("error_dialog_choice")
.Select(e => new ErrorDialog(e))
.ToArray();
ErrorDialogChoice[] edChoices = response
.Element("response_error_dialogs")
.Element("error_dialog_list")
.Element("error_dialog_choice")
.Element("error_dialog_choice_option_list")
.Elements("error_dialog_choice_option")
.Select(e => new ErrorDialogChoice(e))
.ToArray();
So with this example, the first error_dialog_choice object will have a List containing 3 error_dialog_choice_option objects, the second has the two error_dialog_choice_option objects, and any more that may come back. Any help is appreciated. Thanks.
You can use XML serialization to achieve this much easier:
var reader = new StringReader(xmlString);
var ser = new XmlSerializer(typeof(Response));
var result = (Response) ser.Deserialize(reader);
Using these class definitions.
[XmlType("response")]
public class Response
{
[XmlElement("response_error_dialogs")]
public ErrorDialog ErrorDialog;
}
[XmlType("response_error_dialogs")]
public class ErrorDialog
{
[XmlArray("error_dialog_list")]
public List<ChoiceErrorDialog> ChoiceList;
}
[XmlType("error_dialog_choice")]
public class ChoiceErrorDialog
{
[XmlElement("error_dialog_id")]
public int Id;
[XmlElement("error_dialog_message")]
public string Message;
[XmlElement("error_dialog_title")]
public string Title;
[XmlElement("error_dialog_is_set")]
public bool IsSet;
[XmlArray("error_dialog_choice_option_list")]
public List<Option> OptionList;
}
[XmlType("error_dialog_choice_option")]
public class Option
{
[XmlElement("error_dialog_choice_option_id")]
public int Id;
[XmlElement("error_dialog_choice_option_title")]
public string Title;
}
I am guessing there can be more types of error dialogs, and <error_dialog_choice> is just one of the possible types. In this case you could use subclassing, and list the subclasses with XmlArrayItem attributes.
You could also generate the class definitions with xsd.exe or svcutil.exe, from an .xsd or .wsdl file. xsd.exe can even infer the schema from a sample .xml file.
xsd.exe /?
svcutil.exe /?
Use the XmlSerializer built into the framework. Use attributes to map out the xml equivalent of your classes and their properties.
Example:
[XmlElement("Txn")]
public List<Transaction> Items { get; set; }
You can use the XSD.exe to generate an XSD and then generate C# code for your XSD. Use Visual Studio Command Prompt with a sample response.xml file. Eg:
c:>xsd response.xml
c:>xsd response.xsd /c /edb
Just for some fun I was playing with the API of Last.fm. The XML file they return for top artists is structured like this:
<lfm status="ok">
<topartists user="xbonez" type="overall">
<artist rank="1">
<name>Evanescence</name>
<playcount>4618</playcount>
<mbid>f4a31f0a-51dd-4fa7-986d-3095c40c5ed9</mbid>
<url>http://www.last.fm/music/Evanescence</url>
<streamable>1</streamable>
<image size="small">http://userserve-ak.last.fm/serve/34/48488613.png</image>
<image size="medium">http://userserve-ak.last.fm/serve/64/48488613.png</image>
<image size="large">http://userserve-ak.last.fm/serve/126/48488613.png</image>
<image size="extralarge">http://userserve-ak.last.fm/serve/252/48488613.png</image>
<image size="mega">http://userserve-ak.last.fm/serve/500/48488613/Evanescence++PNG.png</image>
</artist>
</topartists>
</lfm>
This is how I'm deserealizing it.
I have a class called lfmStatus:
[Serializable()]
[XmlRootAttribute("lfm")]
public class lfmStatus
{
[XmlElement("artist")]
public List<Artists> TopArtists { get; set; }
}
And a Class Artists:
[Serializable()]
public class Artists
{
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("playcount")]
public int playcount { get; set; }
[XmlElement("url")]
public string url { get; set; }
[XmlElement("streamable")]
public int streamable { get; set; }
[XmlElement("image")]
public string image { get; set; }
}
And then I deserealize using this code:
string XmlFile = "artists.xml";
XmlSerializer serializer = new XmlSerializer(typeof(lfmStatus));
lfmStatus LoadFile;
using (Stream reader = new FileStream(XmlFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
try
{
Console.WriteLine("Beginning deserialization.");
// Call the Deserialize method to restore the object's state.
LoadFile = (lfmStatus)serializer.Deserialize(reader);
return LoadFile.TopArtists;
}
Now, this code works great for the XML if it did not have the topartists tag enveloping all the artists. But since it does, how do I change my code to handle that? I'm assuming I need to add another class.
You are missing the attribute(s) on a few types.
See XmlAttributeAttribute for more detail.
You are also missing the topartists element's type.
If I was you, I would get the XML schema and just use xsd.exe to generate the C# classes, and modify from there. It can also infer the schema based on XML if you really can't find it, this will give you a parsable result based on the input XML.
To see that you wrote correct code for deserializing the response XML you can use XSD. Open VS command prompt and give XSD LastFM.xml which generates and XSD file. Now give XSD LastFM.XSD which will generate a CS file. compare that one with the one you have written and check if you made any mistakes.