XmlDocument - How to get all values from multiple nodes - c#

Here is my sample XML file:
<Main>
<Person>
<Name>Božena</Name>
<Surname>Němcová</Surname>
<Gender>Female</Gender>
<OrderNum>18</OrderNum>
<BirthDate>04.02.1820</BirthDate>
</Person>
<Person>
<Name>Jan</Name>
<Surname>Žižka</Surname>
<Gender>Male</Gender>
<OrderNum>7</OrderNum>
<BirthDate>19.09.1360</BirthDate>
</Person>
<Person>
<Name>Che</Name>
<Surname>Guevara</Surname>
<Gender>Male</Gender>
<OrderNum>27</OrderNum>
<BirthDate>14.06.1928</BirthDate>
</Person>
<Person>
<Name>Antonie</Name>
<Surname>de Saint-Exupéry</Surname>
<Gender>Male</Gender>
<OrderNum>15</OrderNum>
<BirthDate>29.06.1900</BirthDate>
</Person>
</Main>
Here is a code which I want to use to get a list of all values of Name element:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("PersonWrite.xml");
XmlNodeList data = xmlDoc.SelectNodes("Main/Person/Name");
The problem is that I'm only getting a value from the first Person element.

I like using deserialisation, it's a lot easier to work with.
using System;
using System.Xml.Serialization;
using System.IO;
public class Main
{
[XmlElement("Person")]
public Person[] People { get; set; }
}
public class Person
{
public string Name { get; set; }
public string Surname { get; set; }
public string Gender { get; set; }
public int OrderNum { get; set; }
public string BirthDate { get; set; }
}
public class Program
{
public static void Main()
{
var xmlString = #"<Main>
<Person>
<Name>Božena</Name>
<Surname>Němcová</Surname>
<Gender>Female</Gender>
<OrderNum>18</OrderNum>
<BirthDate>04.02.1820</BirthDate>
</Person>
<Person>
<Name>Jan</Name>
<Surname>Žižka</Surname>
<Gender>Male</Gender>
<OrderNum>7</OrderNum>
<BirthDate>19.09.1360</BirthDate>
</Person>
<Person>
<Name>Che</Name>
<Surname>Guevara</Surname>
<Gender>Male</Gender>
<OrderNum>27</OrderNum>
<BirthDate>14.06.1928</BirthDate>
</Person>
<Person>
<Name>Antonie</Name>
<Surname>de Saint-Exupéry</Surname>
<Gender>Male</Gender>
<OrderNum>15</OrderNum>
<BirthDate>29.06.1900</BirthDate>
</Person>
</Main>";
var serializer = new XmlSerializer(typeof (Main));
Main main = null;
using (var reader = new StringReader(xmlString))
{
main = (Main)serializer.Deserialize(reader);
}
if (main == null)
{
return;
}
Console.WriteLine(main.People.Length);
}
}
Output:
4

I would go through the Child Nodes of Main Node
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("PersonWrite.xml");
XmlNodeList root = xmlDoc.SelectNodes("Main");
foreach (XmlNode xnode in root.ChildNodes)
{
//get data from xnode
}

Related

XML serialize multiple elements with same name without namespace

I have the following class which I'm trying to serialize. And I have two arrays with the same data type. I know that if we need more than one element to have the same name the namespace has be different. But there a workaround to remove those namespaces altogether.
[XmlRoot]
public class Album
{
public string Title { get; set; }
public string Description { get; set;}
public int CoverImgIndx { get; set; }
[XmlElement(ElementName ="Element", Namespace ="www.image.com")]
public Image[] Images { get; set; }
[XmlElement(ElementName ="Element", Namespace ="www.cover.com")]
public Image[] Cover { get; set; }
}
public class Image
{
public int indx { get; set; }
public string filepath { get; set; }
}
And I'm using XmlSerializer to serialize this.
public class Program
{
public static void Main()
{
var album = new Album
{
Title = "Album Title",
Description = "Some explanation.",
CoverImgIndx = 2,
Images = new Image[] {
new Image { indx = 0, filepath = #"C:\Images\file1.jpg" },
new Image { indx = 1, filepath = #"C:\Images\file2.png" },
new Image { indx = 2, filepath = #"C:\Images\file3.jpg" }
},
Cover = new Image[] {
new Image { indx = 0, filepath = #"C:\Images\cover1.jpg" }
}
};
XmlSerializer serializer = new XmlSerializer(typeof(Album));
serializer.Serialize(Console.Out, album);
}
}
The output i'm getting there is a namespace for image element. is there way to remove namespace without having to remove Images and Cover shareing the same element name.
<?xml version="1.0" encoding="utf-16"?>
<Album xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Title>Album Title</Title>
<Description>Some explanation.</Description>
<CoverImgIndx>2</CoverImgIndx>
<Element xmlns="www.image.com">
<indx>0</indx>
<filepath>C:\Images\file1.jpg</filepath>
</Element>
<Element xmlns="www.image.com">
<indx>1</indx>
<filepath>C:\Images\file2.png</filepath>
</Element>
<Element xmlns="www.image.com">
<indx>2</indx>
<filepath>C:\Images\file3.jpg</filepath>
</Element>
<Element xmlns="www.cover.com">
<indx>0</indx>
<filepath>C:\Images\cover1.jpg</filepath>
</Element>
</Album>
The output i'm looking for
<?xml version="1.0" encoding="utf-16"?>
<Album xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Title>Album Title</Title>
<Description>Some explanation.</Description>
<CoverImgIndx>2</CoverImgIndx>
<Element>
<indx>0</indx>
<filepath>C:\Images\file1.jpg</filepath>
</Element>
<Element>
<indx>1</indx>
<filepath>C:\Images\file2.png</filepath>
</Element>
<Element>
<indx>2</indx>
<filepath>C:\Images\file3.jpg</filepath>
</Element>
<Element>
<indx>0</indx>
<filepath>C:\Images\cover1.jpg</filepath>
</Element>
</Album>

C# Xml Deserialize, ignore data in XML

During de-serialization, how do I ignore a property? In my case, I don't want the FullName property to get initialized at all. I am not looking for [XMLIgnore] solutions - think it as a scenario where I don't have access to change the class.
Here's my class:
public class Person
{
public int Id { get; set; }
public string FullName { get; set; }
}
Here's how I am initializing:
Person p1 = new Person() { Id = 1, FullName = "P1" };
Person p2 = new Person() { Id = 2, FullName = "P2" };
List<Person> persons = new List<Person> { p, q }; //this object is xml serialized.
And here's my XML: ( I got it though the XML Serialization)
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Person>
<Id>1</Id>
<FullName>P1</FullName>
</Person>
<Person>
<Id>2</Id>
<FullName>P2</FullName>
</Person>
</ArrayOfPerson>
You can use a custom XmlReader in the deserialization process that will simply skip over the FullName elements. Something like this:
public class MyXmlReader : XmlTextReader
{
public MyXmlReader(string filePath) : base(filePath)
{
}
public override bool Read()
{
if (base.Read())
{
if (Name == "FullName")
return base.Read();
return true;
}
return false;
}
}
Then use it like this
var serializer = new XmlSerializer(typeof(List<Person>));
using (var reader = new MyXmlReader("XMLFile.xml"))
{
var person = (List<Person>)serializer.Deserialize(reader);
}
You can implement a different constructor to take a stream or whatever you have. It doesn't have to be a file path.

Displaying a class list on the main static void

I'm looking to output the list that I've made and usually I just do a for each loop and call up the list like shown in the code below. The console wants me to correct it but then it doesn't display as the reference is null.
Thanks for any help
class Program
{
const string FILENAME = #"royalTreeResults.xml";
// THIS SECTION OF CODE IS WHAT IT SUGGESTS private static readonly IEnumerable<Family> families;
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
Tree tree = new Tree();
tree.families = doc.Descendants("family").Select(x => new Family()
{
FamilyName = (string)x.Element("name"),
FamilyTotalReign = (int)x.Element("totalReign"),
People = x.Elements("person").Select(y => Person.Recursive(y)).ToList()
}).ToList();
foreach(Family per in families) // <--- THE ERROR IS HERE
{
Console.WriteLine(per.FamilyName + " " + per.FamilyTotalReign + " " + per.People);
}
}
}
public class Family
{
public string FamilyName { get; set; }
public int FamilyTotalReign { get; set; }
public List<Person> People { get; set; }
}
public class Person
{
public int? PersonBorn { get; set; }
public int? PersonCoronation { get; set; }
public int? PersonDied { get; set; }
public string PersonName { get; set; }
public int? PersonYinPower { get; set; }
public List<Person> Children { get; set; }
public static Person Recursive(XElement person)
{
Person newPerson = new Person();
newPerson.PersonName = (string)person.Element("name");
newPerson.PersonYinPower = (int?)person.Element("yearsInpower");
newPerson.PersonBorn = (int?)person.Element("born");
newPerson.PersonDied = (int?)person.Element("died");
newPerson.PersonCoronation = (int?)person.Element("coronation");
if (person.Element("children") != null)
{
newPerson.Children = person.Element("children").Elements("person").Select(y => Person.Recursive(y)).ToList();
}
return newPerson;
}
}
public class Tree
{
public List<Family> families = new List<Family>();
}
A sample of the .xml is below, however I know this is not the problem
<?xml version="1.0" encoding="utf-8"?>
<royaltree>
<family>
<name>Wessex</name>
<totalReign>137</totalReign>
<person>
<name>Alfred the Great</name>
<yearsInpower>28</yearsInpower>
<born>849</born>
<died>899</died>
<coronation>871</coronation>
<children>
<person>
<name>Edward the Elder</name>
<yearsInpower>25</yearsInpower>
<born>879</born>
<died>924</died>
<coronation>899</coronation>
<children>
<person>
<name>Edmund I the Elder</name>
<yearsInpower>6</yearsInpower>
<born>939</born>
<died>946</died>
<coronation>940</coronation>
<children>
<person>
<name>Edger the Peaceful</name>
<yearsInpower>16</yearsInpower>
<born>944</born>
<died>975</died>
<coronation>959</coronation>
<children>
<person>
<name>Ethelred II</name>
<yearsInpower>38</yearsInpower>
<born>962</born>
<died>1016</died>
<coronation>978</coronation>
<children>
<person>
<name>Edward Confessor</name>
<yearsInpower>24</yearsInpower>
<born>1002</born>
<died>1066</died>
<coronation>1042</coronation>
</person>
<person>
<name>Edward II Ironside</name>
<yearsInpower>0</yearsInpower>
<born>1002</born>
<died>1016</died>
<coronation>1016</coronation>
</person>
</children>
</person>
</children>
</person>
</children>
</person>
</children>
</person>
</children>
</person>
</family>
<family>
<name>Norman</name>
<totalReign>69</totalReign>
<person>
<name>William I</name>
<yearsInpower>21</yearsInpower>
<born>1028</born>
<died>1087</died>
<coronation>1066</coronation>
<children>
<person>
<name>Adela</name>
<born>1050</born>
<died>1080</died>
</person>
<person>
<name>William II</name>
<yearsInpower>13</yearsInpower>
<born>1056</born>
<died>1100</died>
<coronation>1087</coronation>
</person>
<person>
<name>Henry I Beauclerc</name>
<yearsInpower>35</yearsInpower>
<born>1068</born>
<died>1135</died>
<coronation>1100</coronation>
<children>
<person>
<name>Matilda</name>
<born>1130</born>
<died>1167</died>
</person>
</children>
</person>
</children>
</person>
</family>
I don't know if I get you right. But I hope this might be helpful:
static void Main(string[] args)
{
//... your stuff
//Iterate through families in tree
foreach (Family per in tree.families)
{
//Show the family info
Console.WriteLine(per.FamilyName + " " + per.FamilyTotalReign);
//Show the members recursively
ShowPeopleRecursive(per.People, 1);
}
}
//Shows the people tree.
private static void ShowPeopleRecursive(List<Person> people, int level)
{
const string indent = "";
foreach (var p in people)
{
Console.WriteLine($"{indent.PadLeft(level, ' ')} {p.PersonName} ({p.PersonBorn} - {p.PersonDied}). Years in power: {p.PersonYinPower}");
if (p.Children != null)
ShowPeopleRecursive(p.Children, level + 1);
}
}
You iterate over your families in the tree and then recursively print the people (and their children). You should see something like:
Hope this helps!
So you have to loop through you person list like below.
foreach (Family per in tree.families) // <--- THE ERROR IS HERE
{
Console.WriteLine(per.FamilyName + " " + per.FamilyTotalReign);
foreach (var ppl in per.People)
{
Console.WriteLine(ppl.PersonName + " " + per.FamilyName);
}
}
But I would like to recommend you to use XmlSerializer and you can Deserialize your XML to object.This is much more cleaner approach. if you want sample code let me know.

I need to get the childnodes of a parent in a list in XML so that I can put them in a loop to put all values in Excel

I am sending one XML string as a webservices, and I want to interpret it. My XML string is like this:
<?xml version="1.0" encoding="utf-8"?>
<result is_array="true">
<item>
<candidate_offer_id>175</candidate_offer_id><contact_person>Ranjeet Singh</contact_person><offer_status>8</offer_status>
</item>
<item><candidate_offer_id>176</candidate_offer_id><contact_person>Ranjeet Singh</contact_person><offer_status>8</offer_status>
</item>
</result>
In this XML string I want to access the child node like candidate_offer_id, offer_status name under node <item> in a list so that later I can run a loop to get all these values in a loop and put it on the Excel sheet. Until now I have written like this:
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(responseStream);
string responseFromServer = reader.ReadToEnd();
StringBuilder output = new StringBuilder();
var str = XElement.Parse(responseFromServer);
var result = str.Element("item");
But how do I interpert the var result to get the tag name of parent node <item> in a list and also how to interpret it?
You can also use XmlDocument class to load and manipulate the XML.
XmlDocument doc = new XmlDocument();
//doc.Load(responseStream); -- Stream loading
doc.LoadXml(responseFromServer); // where responseFromServer is a xml string
XmlNodeList list = doc.DocumentElement.SelectNodes("//item/*");
foreach (XmlNode n in list)
{
Console.WriteLine("{0} : {1}", n.Name, n.Value);
}
// As a list object that can be converted further
IEnumerable<XmlNode> node = list.Cast<XmlNode>();
I am presuming that the <code> tags are in error; if not, the xml will need correcting before if can be used.
* Update : Simplified the code to extract child nodes as list
First, your XML is invalid. You want <code> after <?xml> like:
<?xml version=""1.0"" encoding=""utf-8""?>
<code>
<result is_array=""true"">
<item>
<candidate_offer_id>175</candidate_offer_id><contact_person>Ranjeet Singh</contact_person><offer_status>8</offer_status>
</item>
<item><candidate_offer_id>176</candidate_offer_id><contact_person>Ranjeet Singh</contact_person><offer_status>8</offer_status>
</item>
</result>
</code>
I'd make a class to contain each item like
static void Main(string[] args)
{
var str = XElement.Parse(xml);
var items = str.Descendants("item");
List<Item> Items = new List<Item>();
foreach (var item in items)
{
Items.Add(new Item
{
OfferID = Convert.ToInt32(item.Element("candidate_offer_id").Value),
Person = item.Element("contact_person").Value,
Status = Convert.ToInt32(item.Element("offer_status").Value)
});
}
}
class Item
{
public int OfferID { get; set; }
public string Person { get; set; }
public int Status { get; set; }
}
XDocument will work for this case since you're not using the entire XML structure.
Assuming your XML is valid such as:
<?xml version="1.0" encoding="UTF-8"?>
<result is_array="true">
<item>
<candidate_offer_id>175</candidate_offer_id>
<contact_person>Ranjeet Singh</contact_person>
<offer_status>8</offer_status>
</item>
<item>
<candidate_offer_id>176</candidate_offer_id>
<contact_person>Ranjeet Singh</contact_person>
<offer_status>8</offer_status>
</item>
</result>
DTO:
public class CandidateOffer
{
public int CandidateOfferId { get; set; }
public string ContactPerson { get; set; }
public int OfferStatus { get; set; }
}
Parser:
public CandidateOffer ParseCandidateOffer(XElement element)
{
int candidateOfferId;
if(!int.TryParse(element.Element("candidate_offer_id").Value,
out candidateOfferId))
{
candidateOfferId = 0;
}
var contactPerson = element.Element("contact_person").Value;
int offerStatus;
if(!int.TryParse(element.Element("offer_status").Value,
out offerStatus))
{
offerStatus = 0;
}
return new CandidateOffer
{
CandidateOfferId = candidateOfferId,
ContactPerson = contactPerson,
OfferStatus = offerStatus
};
}
Usage:
var xDocument = XDocument.Parse(xmlString);
var candidateOffers = xDocument.XPathSelectElements("//item")
.Select(ParseCandidateOffer);
foreach(var candidateOffer in candidateOffers)
{
Console.WriteLine(candidateOffer.CandidateOfferId);
}

Correct way to convert Xml to Objects?

I have very simple Xml structure that I want to convert to list of objects. My code does work but I think this is not the correct way of doing this and since I never did this I think there might be simpler way of doing what I want.
Xml example
<root>
<item>
<name>Item 1</name>
<price>30.00</price>
</item>
<item>
<name>Item 2</name>
<price>55.00</price>
</item>
</root>
Code to gather xml and create list of objects
class Program
{
static void Main(string[] args)
{
List<Item> itemList = new List<Item>();
var url = "http://xmlurl.com/xml";
// Load xml data
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(url);
// Select items and loop
var xmlItems = myXmlDocument.SelectNodes("/root/item");
foreach (XmlNode item in xmlItems)
{
var newItem = new Item();
foreach (XmlNode i in item)
{
// Since I cannot query them properly I need to check every item node
switch (i.Name)
{
case "name":
newItem.Name = i.InnerText;
break;
case "price":
newItem.Price = Convert.ToDecimal(i.InnerText);
break;
}
}
itemList.Add(newItem);
}
// Test it out
foreach (var item in itemList.OrderBy(x => x.Price))
{
Console.WriteLine(item.Name + " | " + item.Price);
}
Console.ReadLine();
}
}
class Item
{
public string Name { get; set; }
public decimal Price { get; set; }
}
Using LINQ:
XDocument xdoc = XDocument.Load("myXml.xml");
List<Item> items = (from item in xdoc.Descendants("item")
select new Item {
Name = item.Element("name").Value,
Price = item.Element("price").Value
}).ToList();
You should use XmlSerializer, example:
Classes:
[XmlType(TypeName="item")]
public class Item {
public string Name { get; set; }
public decimal Price { get; set; }
}
[XmlRoot(ElementName = "root")]
public class ItemList : List<Item> {
}
Getting them from markup:
const string test = #"<root>
<item>
<name>Item 1</name>
<price>30.00</price>
</item>
<item>
<name>Item 2</name>
<price>55.00</price>
</item>
</root>";
var serializer = new XmlSerializer(typeof(ItemList));
List<Item> result;
using (var reader = new StringReader(test)) {
result = (List<Item>)serializer.Deserialize(reader);
}
You can define your class:
[XmlType("item")]
public class Item
{
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("price")]
public decimal Price { get; set; }
}
And then deserialize the Xml:
var xml = #"<root>
<item>
<name>Item 1</name>
<price>30.00</price>
</item>
<item>
<name>Item 2</name>
<price>55.00</price>
</item>
</root>";
List<Item> items;
var serializer = new XmlSerializer(typeof(List<Item>),
new XmlRootAttribute("root"));
using(var stream = new StringReader(xml))
{
items = (List<Item>)serializer.Deserialize(stream);
}
if(items != null)
{
foreach(var item in items)
{
Console.Write(item);
}
}

Categories

Resources