Linq to XML data structure - c#

This is my XML:
<home>
<contents>
<row>
<content>
<idContent>1</idContent>
<title>title1</title>
</content>
<content>
<idContent>2</idContent>
<title>title2</title>
</content>
</row>
<row>
<content>
<idContent>3</idContent>
<title>title3</title>
</content>
<content>
<idContent>4</idContent>
<title>title4</title>
</content>
</row>
</contents>
I want to store this information in a list of objects
List myList = ...
Where a Content could be:
int idContent;
string title;
int row_number;
Each Content object has to store the row where it is located in the XML.
What's the best way for doing this?

Presuming row_number is simply a sequence relating to the order it appears in the XML, then you can do something like this:
var doc = XDocument.Parse(xml);
var contents = doc.Descendants("row")
.Select((e, index) => new {Row = e, RowIndex = index})
.SelectMany(x => x.Row.Elements("content").Select(e => new {Content = e, x.RowIndex}))
.Select(x => new Content
{
IdContent = (int)x.Content.Element("idContent"),
Title = (string)x.Content.Element("title"),
RowNumber = x.RowIndex + 1
}).ToList();

I use this method for similar scenarios:
public static Object CreateObject(string XMLString, Object YourClassObject)
{
System.Xml.Serialization.XmlSerializer oXmlSerializer = new System.Xml.Serialization.XmlSerializer(YourClassObject.GetType());
//The StringReader will be the stream holder for the existing XML file
YourClassObject = oXmlSerializer.Deserialize(new System.IO.StringReader(XMLString));
//initially deserialized, the data is represented by an object without a defined type
return YourClassObject;
}
With this method you can create a class object from a XML String. I haven't tested it on your scenario, but you can use it on object of following home class:
public class home
{
public List<row> contents;
}
public class row
{
public List<content> content;
}
public class content
{
public int idContent;
public string title;
}
USAGE:
home h = new home();
h = (home)CreateObject(xml, h);
Remember that the variable and class names must be exactly same as that of XML nodes.
EXTRA:
If you want to convert a class object into XML String, use this method:
string CreateXML(Object YourClassObject)
{
XmlDocument xmlDoc = new XmlDocument(); //Represents an XML document,
// Initializes a new instance of the XmlDocument class.
XmlSerializer xmlSerializer = new XmlSerializer(YourClassObject.GetType());
// Creates a stream whose backing store is memory.
using (MemoryStream xmlStream = new MemoryStream())
{
xmlSerializer.Serialize(xmlStream, YourClassObject);
xmlStream.Position = 0;
//Loads the XML document from the specified string.
xmlDoc.Load(xmlStream);
return xmlDoc.InnerXml;
}
}

The best solution I could think of - although it is not using Linq to XML - is to feed your XML document into one of those various XML Schema generators like http://www.freeformatter.com/xsd-generator.html and pipe this generated Schema right into xsd.exe (see https://msdn.microsoft.com/en-us/library/x6c1kb0s.aspx). The generated code can be used to read (and write) the XML document such as the one you provided and you can of course apply Linq to Objects on this collection.
However, since you mentioned the row_number field which does not yet appear in your example XML, you would have to add this to the XML or manually edit the XML Schema afterwards.

Related

Deserializing a class into a file and back into a list

I am trying to deserialize a csv file into a list.
If there is a better way of sending my class' data into a file and then putting it back together into a list, please do let me know.
The class:
[Serializable]
public class Student
{
int studentID;
string studentName;
string studentAge;
}
The list:
List<Student> students = new List<Student>();
students.studentID = txt_sID.Text;
students.studentName = txt_sName.Text;
students.studentAge = txt_sAge.Text;
Serializer:
XmlSerializer SerializerObj = new XmlSerializer(typeof(Student));
TextWriter WriteFileStream = new StreamWriter(#"students.csv", true);
SerializerObj.Serialize(WriteFileStream, students);
WriteFileStream.Close();
Deserializer:
XmlSerializer serializer = new XmlSerializer(typeof(Student));
using (Stream reader = new FileStream(#"students.csv", FileMode.Open))
{
Student students2 = (Student)serializer.Deserialize(reader);
students.Add(students2);
}
Error:
XmlException: Unexpected XML declaration. The XML declaration must be the first node in the document, and no whitespace characters are allowed to appear before it.
The CSV file contents:
<?xml version="1.0" encoding="utf-8"?>
<Student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<studentID>1</studentID>
<studentName>Jonathan Joestar</studentName>
<studentAge>21</studentAge>
</Student><?xml version="1.0" encoding="utf-8"?>
What I have tried:
use XmlWriter.Create as a serializer to get rid of the Xml tags. The csv file must accept multiple entries and be put back into a list, which XmlWriter.Create can only do with one entry at one time (?).
Kind regards and thank you for your time.
I would suggest ignoring CSV and focusing on XML. CSV files essentially represents a table, and this cannot represent arbitrary object hierarchies. So in that aspect xml is more flexible.
To serialize a list of objects you should specify a list type when serializing. I.e. List<Student> or Student[]. For a complete example:
[Serializable]
public class Student
{
public int Id;
public string Name;
public float Age;
public Student() { }
public Student(int id, string name, float age)
{
Id = id;
Name = name;
Age = age;
}
}
public void SerializeDeserialize()
{
var xmlSerializer = new XmlSerializer(typeof(Student[]));
var ms = new MemoryStream();
var students = new Student[]
{
new Student(1, "Bob", 42),
new Student(2, "Alice", 22.4f)
};
xmlSerializer.Serialize(ms, students);
ms.Position = 0; // Reset stream to allow it to be deserialized
var deserialized = (Student[])xmlSerializer.Deserialize(ms);
}
Replace the memory stream with a file stream you want to save to a file.
Note that while xml is perfectly fine, most of the industry are moving to json. As far as I know json does not have any real technical advantage, so its more a issue of being less verbose and having serialization libraries that are a bit better.

Retrieve values from XML File

I have Multiple XML Files that look like below
<?xml version="1.0" encoding="UTF-8"?>
<schema>
<sp_transaction_id name="sp_transaction_id" value="1" />
<sp_year name="sp_year" value="2015" />
<sp_first_name name="sp_first_name" value="James" />
<sp_gender name="sp_gender" value="Male" />
<sp_date_of_birth name="sp_date_of_birth" value="06-06-1999" />
</schema>
The XML Format i think is in Key-Value Pairs.
I want to extract these values and store it into a database(SQL Server 2012) table, with the name(eg; sp_year) as Column Name and value(eg; 2015) as the Column value using ASP.NET C#.
I think i can upload the file and read it like this :
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string filePath = Server.MapPath("~/Uploads/") + fileName;
FileUpload1.SaveAs(filePath);
string xml = File.ReadAllText(filePath);
But thats pretty much it ( Sorry Im a beginner ). Please Guide me. Thanks
You can use the following code to get the key value pairs
XDocument doc = XDocument.Load(filePath);
var schemaElement = doc.Element("schema");
foreach (var xElement in schemaElement.Elements())
{
Console.WriteLine(xElement.Attribute("name").Value + ":" + xElement.Attribute("value").Value);
}
Elements method returns all elements inside schema element.
However I suggest changing xml file to this format, if possible
<?xml version="1.0" encoding="UTF-8"?>
<schema>
<KeyValuePair name="sp_transaction_id" value="1" />
<KeyValuePair name="sp_year" value="2015" />
<KeyValuePair name="sp_first_name" value="James" />
<KeyValuePair name="sp_gender" value="Male" />
<KeyValuePair name="sp_date_of_birth" value="06-06-1999" />
</schema>
For reading data from an xml file you don't need to upload it.You can give path of xml and read from it.You can use following method to read from xml
public static XmlDocument LoadXmlDocument(string xmlPath)
{
if ((xmlPath == "") || (xmlPath == null) || (!File.Exists(xmlPath)))
return null;
StreamReader strreader = new StreamReader(xmlPath);
string xmlInnerText = strreader.ReadToEnd();
strreader.Close();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlInnerText);
return xmlDoc;
}
For reading data from xml you can use
XmlDocument xmlDoc = LoadXmlDocument(xmlPath);
XmlNodeList nodes = xmlDoc .SelectNodes("//*");
foreach (XmlElement node in nodes)
{
.
.
.
}
In foreach loop you can get your required values e.g sp_year
The answer below shows how to create an XmlDocument from it.
I would suggest to use it as a User-Defined class if you know whether Xml schema change.
First of all you should create POCO class appropriate to Xml file schema using XmlAnnotations.
Secondly:
Having path of the file:
XmlSerializer serializer = new XmlSerializer(typeof(definedclass));
using (FileStream fs = File.Open(pathtofile))
using (XmlReader reader = XmlReader.Create(fs))
{
var xmlObject = serializer.Deserialize(reader);
}
xmlObject is now your user-defined class with values from xml.
Regards,
Rafal
You can load the files into an XDocument & then use Linq-To-XML to extract the required information. The example code below loads all name/value pairs into an array of class :
class MyXMLClass
{
public String FieldName { get; set; }
public String Value { get; set; }
}
The code gets all "schema" descendants (just one as it is the top level element), then selects all elements inside the & creates a new class object for each extracting the name & value.
XDocument xd = XDocument.Load("test.xml");
MyXMLClass[] xe =
xd.Descendants("schema")
.Elements()
.Select(n => new MyXMLClass {FieldName = n.Attribute("name").Value, Value = n.Attribute("value").Value})
.ToArray();

Parsing XML using C# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Amazon Marketplace XML parsing
I am new to parsing XML in C# and I have some data from Amazon MWS library that is displayed below. I need to parse out various ItemAttributes such as ItemDimensions. I am use to JSON responses so I am not sure how to apply this to XML. Would it be possible from someone to point me in the right direction? I have Googled XML Parsing with C# but not valuable results were found to help me complete my tasks.
<GetMatchingProductResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetMatchingProductResult ASIN="1430225491" status="Success">
<Product>
<Identifiers>
<MarketplaceASIN>
<MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
<ASIN>1430225491</ASIN>
</MarketplaceASIN>
</Identifiers>
<AttributeSets>
<ns2:ItemAttributes xml:lang="en-US">
<ns2:Author>Troelsen, Andrew</ns2:Author>
<ns2:Binding>Paperback</ns2:Binding>
<ns2:Brand>Apress</ns2:Brand>
<ns2:Edition>5</ns2:Edition>
<ns2:ItemDimensions>
<ns2:Height Units="inches">9.21</ns2:Height>
<ns2:Length Units="inches">7.48</ns2:Length>
<ns2:Width Units="inches">2.52</ns2:Width>
<ns2:Weight Units="pounds">5.80</ns2:Weight>
</ns2:ItemDimensions>
<ns2:IsAutographed>false</ns2:IsAutographed>
<ns2:IsEligibleForTradeIn>true</ns2:IsEligibleForTradeIn>
<ns2:IsMemorabilia>false</ns2:IsMemorabilia>
<ns2:Label>Apress</ns2:Label>
<ns2:Languages>
<ns2:Language>
<ns2:Name>english</ns2:Name>
<ns2:Type>Unknown</ns2:Type>
</ns2:Language>
<ns2:Language>
<ns2:Name>english</ns2:Name>
<ns2:Type>Original Language</ns2:Type>
</ns2:Language>
<ns2:Language>
<ns2:Name>english</ns2:Name>
<ns2:Type>Published</ns2:Type>
</ns2:Language>
</ns2:Languages>
<ns2:ListPrice>
<ns2:Amount>59.99</ns2:Amount>
<ns2:CurrencyCode>USD</ns2:CurrencyCode>
</ns2:ListPrice>
<ns2:Manufacturer>Apress</ns2:Manufacturer>
<ns2:NumberOfItems>1</ns2:NumberOfItems>
<ns2:NumberOfPages>1752</ns2:NumberOfPages>
<ns2:PackageDimensions>
<ns2:Height Units="inches">2.60</ns2:Height>
<ns2:Length Units="inches">9.20</ns2:Length>
<ns2:Width Units="inches">7.50</ns2:Width>
<ns2:Weight Units="pounds">5.80</ns2:Weight>
</ns2:PackageDimensions>
<ns2:PartNumber>9781430225492</ns2:PartNumber>
<ns2:ProductGroup>Book</ns2:ProductGroup>
<ns2:ProductTypeName>ABIS_BOOK</ns2:ProductTypeName>
<ns2:PublicationDate>2010-05-14</ns2:PublicationDate>
<ns2:Publisher>Apress</ns2:Publisher>
<ns2:SmallImage>
<ns2:URL>http://ecx.images-amazon.com/images/I/51h9Sju5NKL._SL75_.jpg</ns2:URL>
<ns2:Height Units="pixels">75</ns2:Height>
<ns2:Width Units="pixels">61</ns2:Width>
</ns2:SmallImage>
<ns2:Studio>Apress</ns2:Studio>
<ns2:Title>Pro C# 2010 and the .NET 4 Platform</ns2:Title>
</ns2:ItemAttributes>
</AttributeSets>
<Relationships/>
<SalesRankings>
<SalesRank>
<ProductCategoryId>book_display_on_website</ProductCategoryId>
<Rank>43011</Rank>
</SalesRank>
<SalesRank>
<ProductCategoryId>697342</ProductCategoryId>
<Rank>36</Rank>
</SalesRank>
<SalesRank>
<ProductCategoryId>3967</ProductCategoryId>
<Rank>53</Rank>
</SalesRank>
<SalesRank>
<ProductCategoryId>4013</ProductCategoryId>
<Rank>83</Rank>
</SalesRank>
</SalesRankings>
</Product>
</GetMatchingProductResult>
<ResponseMetadata>
<RequestId>440cdde0-fa76-4c48-bdd1-d51a3b467823</RequestId>
</ResponseMetadata>
</GetMatchingProductResponse>
I find "Linq To Xml" easier to use
var xDoc = XDocument.Parse(xml); //or XDocument.Load(filename);
XNamespace ns = "http://mws.amazonservices.com/schema/Products/2011-10-01";
var items = xDoc.Descendants(ns + "ItemAttributes")
.Select(x => new
{
Author = x.Element(ns + "Author").Value,
Brand = x.Element(ns + "Brand").Value,
Dimesions = x.Element(ns+"ItemDimensions").Descendants()
.Select(dim=>new{
Type = dim.Name.LocalName,
Unit = dim.Attribute("Units").Value,
Value = dim.Value
})
.ToList()
})
.ToList();
You could reinvent the wheel, or you could use Amazon's wheel (see #George Duckett's answer for the direct link):
Amazon Marketplace API
One option to address your question: if you want a tool that will enable you to work with your xml file, I would look at xsd.exe. MSDN for xsd.exe
This tool is able to generate classes from xml.
Otherwise, you can create a parser from the XDocument class that will allow you to use linq to build a parser such as #L.B noted in his post.
You have not made clear exactly what you need from the XML, so I cannot give you an objective answer. I'll begin by stating that there are many different ways to parse XML using .Net (and C# in your case, albeit they are similar with VB and C#).
The first one that I would look into is actually modeling your XML Data into .Net objects, more specifically, POCOs. To that class model you could add attributes that would bind or relate them to the XML and then all you'd need to do is pass the data and the class to a XML deserializer.
Now, if you don't need to retrieve the whole object, you can either use XDocument or XmlDocument. The fun part of XDocument is that its syntax in LINQ friendly, so you can parse you XML very simply.
XmlDocument is more old-style sequential method invocation, but achieves the same thing.
Let me illustrate. Consider a simpler XML, for simplicity sake's:
<body>
<h1>This is a text.</h1>
<p class="SomeClass">This is a paragraph</p>
</body>
(see what I did there? That HTML is a valid XML!)
I. Using A Deserializer:
First you model the classes:
[XmlRoot]
public class body
{
[XmlElement]
public h1 h1 { get; set; }
[XmlElement]
public p p { get; set; }
}
public class h1
{
[XmlText]
public string innerXML { get; set; }
}
public class p
{
[XmlAttribute]
public string id { get; set; }
[XmlText]
public string innerXML { get; set; }
}
Once you have your class model, you call the serializer.
void Main()
{
string xml =
#"<body>
<h1>This is a text.</h1>
<p id=""SomeId"">This is a paragraph</p>
</body>";
// Creates a stream that reads from the string
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(xml);
writer.Flush();
stream.Position = 0;
// Check the classes below before proceding.
XmlSerializer serializer = new XmlSerializer(typeof(body));
var obj = (body)serializer.Deserialize(stream);
// Check obj here with the debugger. All fields are filled.
}
II. Using XDocument
The example above makes for a very neat code, since you access everything typed. However, it demands a lot of setup work since you must model the classes. Maybe some simpler will suffice in your case. Let's say you want to get the attribute of the p element:
void Main()
{
string xml =
#"<body>
<h1>This is a text.</h1>
<p id=""SomeId"">This is a paragraph</p>
</body>";
// Creates a stream that reads from the string
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(xml);
writer.Flush();
stream.Position = 0;
// Using XDocument
var pAttrib = XDocument.Load(stream).Element("body").Element("p").Attribute("id").Value;
Console.Writeline(pAttrib);
}
Simple, huh? You can do more complex stuff throwing LINQ there... Let's try to find the element with id named "SomeId":
void Main()
{
string xml =
#"<body>
<h1>This is a text.</h1>
<p id=""SomeId"">This is a paragraph</p>
</body>";
// Creates a stream that reads from the string
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(xml);
writer.Flush();
stream.Position = 0;
// Using XDocument
var doc = XDocument.Load(stream);
var found = from body in doc.Elements("body")
from elem in body.Elements()
from attrib in elem.Attributes()
where attrib.Name == "id" && attrib.Value == "SomeId"
select elem;
foreach (var e in found) Console.WriteLine(e);
}
Hope it helps.

Get filtered List<T> from XML

I am using following code to get List from xml file -
public static List<T> GetListFromXml<T>(string filePath)
{
using (TextReader reader = new StreamReader(filePath))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
return (List<T>) serializer.Deserialize(reader);
}
}
However, I also need a way of filtering records where I can filter list by -
- no. of records to take from xml file
- filter by a particular node value
So signature of above method will change to -
public static List<T> GetListFromXml<T>(string filePath,
int listCount,
string filterbyNode,
string filterByValue);
Please guide me if I can filter directly from XML file or I should filter from returned list?
P.S.
The xml file mentioned above is also created from code using -
public static void WriteListToXml<T>(List<T> list, string filePath)
{
using (TextWriter writer = new StreamWriter(filePath))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
serializer.Serialize(writer, list);
}
}
Why I am saving my custom collection returned from database to xml file - because I want to process only a batch of records at a time.
and XML file snippet (generated from above code) -
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfClassifiedLocation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CLocation>
<CId>5726</CId>
<Long>0</Long>
<Lat>0</Lat>
<Postcode>ZZ1 5ZZ</Postcode>
<Street />
<Town />
</CLocation>
<CLocation>
<CId>5736</CId>
<Long>0</Long>
<Lat>0</Lat>
<Postcode>ZZ1 5ZZ</Postcode>
<Street />
<Town />
</CLocation>
</ArrayOfClassifiedLocation>
If you have access to .net >= 3.5, you should consider using Linq to Xml
Below an example of LINQ-to-XML query which consider cid paremeter filter. If you put empty string in cidFilter the query would return all entries:
// you can use Parse() method:
// XDocument loaded = XDocument.Parse(xmlString);
XDocument loaded = XDocument.Load("c:\\temp\\testxl.xml");
// return only entry fith CID == 5726
string cidFilter = "5726";
var filteredItems =
loaded.Descendants("CLocation")
.Select((w, i) =>
new
{
Index = i,
CID = w.Descendants("CId").FirstOrDefault().Value,
PostCode = w.Descendants("Postcode").FirstOrDefault().Value
})
.Where(item => String.IsNullOrEmpty(cidFilter) || item.CID == cidFilter)
.ToList();
int itemsCount = filteredItems.Count();

C# newbie: reading repetitive XML to memory

I'm new to C#. I'm building an application that persists an XML file with a list of elements. The structure of my XML file is as follows:
<Elements>
<Element>
<Name>Value</Name>
<Type>Value</Type>
<Color>Value</Color>
</Element>
<Element>
<Name>Value</Name>
<Type>Value</Type>
<Color>Value</Color>
</Element>
<Element>
<Name>Value</Name>
<Type>Value</Type>
<Color>Value</Color>
</Element>
</Elements>
I have < 100 of those items, and it's a single list (so I'm considering a DB solution to be overkill, even SQLite). When my application loads, I want to read this list of elements to memory. At present, after browsing the web a bit, I'm using XmlTextReader.
However, and maybe I'm using it in the wrong way, I read the data tag-by-tag, and thus expect the tags to be in a certain order (otherwise the code will be messy). What I would like to do is read complete "Element" structures and extract tags from them by name. I'm sure it's possible, but how?
To clarify, the main difference is that the way I'm using XmlTextReader today, it's not tolerant to scenarios such as wrong order of tags (e.g. Type comes before Name in a certain Element).
What's the best practice for loading such structures to memory in C#?
It's really easy to do in LINQ to XML. Are you using .NET 3.5? Here's a sample:
using System;
using System.Xml.Linq;
using System.Linq;
class Test
{
[STAThread]
static void Main()
{
XDocument document = XDocument.Load("test.xml");
var items = document.Root
.Elements("Element")
.Select(element => new {
Name = (string)element.Element("Name"),
Type = (string)element.Element("Type"),
Color = (string)element.Element("Color")})
.ToList();
foreach (var x in items)
{
Console.WriteLine(x);
}
}
}
You probably want to create your own data structure to hold each element, but you just need to change the "Select" call to use that.
Any particular reason you're not using XmlDocument?
XmlDocument myDoc = new XmlDocument()
myDoc.Load(fileName);
foreach(XmlElement elem in myDoc.SelectNodes("Elements/Element"))
{
XmlNode nodeName = elem.SelectSingleNode("Name/text()");
XmlNode nodeType = elem.SelectSingleNode("Type/text()");
XmlNode nodeColor = elem.SelectSingleNode("Color/text()");
string name = nodeName!=null ? nodeName.Value : String.Empty;
string type = nodeType!=null ? nodeType.Value : String.Empty;
string color = nodeColor!=null ? nodeColor.Value : String.Empty;
// Here you use the values for something...
}
It sounds like XDocument, and XElement might be better suited for this task. They might not have the absolute speed of XmlTextReader, but for your cases they sound like they would be appropriate and it would make dealing with fixed structures a lot easier. Parsing out elements would work like so:
XDocument xml;
foreach (XElement el in xml.Element("Elements").Elements("Element")) {
var name = el.Element("Name").Value;
// etc.
}
You can even get a bit fancier with Linq:
XDocument xml;
var collection = from el in xml.Element("Elements").Elements("Element")
select new { Name = el.Element("Name").Value,
Color = el.Element("Color").Value,
Type = el.Element("Type").Value
};
foreach (var item in collection) {
// here you can use item.Color, item.Name, etc..
}
You could use XmlSerializer class (http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx)
public class Element
{
public string Name { get; set; }
public string Type { get; set; }
public string Color { get; set; }
}
class Program
{
static void Main(string[] args)
{
string xml =
#"<Elements>
<Element>
<Name>Value</Name>
<Type>Value</Type>
<Color>Value</Color>
</Element>(...)</Elements>";
XmlSerializer serializer = new XmlSerializer(typeof(Element[]), new XmlRootAttribute("Elements"));
Element[] result = (Element[])serializer.Deserialize(new StringReader(xml));}
You should check out Linq2Xml, http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx

Categories

Resources