I have no idea what you mean by "
show only the missing element nodes in the input Xml comparing with the standard Xml", especially what
standard xml
is... Nevertheless...
There are - at least - two solutions:
1) XML validation using schema (XSD file)
How to: Validate Using XSD (LINQ to XML)[
^]
XML Schema (XSD) Validation with XmlSchemaSet[
^]
XmlDocument.Validate Method (ValidationEventHandler)[
^]
2) Data comparison using custom class (and class collection) and IEqualityComparer<T>[^] and Enumerable.Except()[^] method:
Second method is bit harder to explain, so... the best method is to show an example (please read comments).
Imagine, you have a list of
Book
s with the following properties:
Id
,
Title
,
Author
. Example class may look like:
public class Book
{
private int id = 0;
private string author = string.Empty;
private string title = string.Empty;
public Book(int _id, string _author, string _title)
{
id = _id;
author = _author;
title = _title;
}
public int Id
{
get { return id; }
set { id = value; }
}
public string Author
{
get { return author; }
set { author = value; }
}
public string Title
{
get { return title; }
set { title = value; }
}
}
IEqualityComparer class may look like:
public class BookComparer : IEqualityComparer<Book>
{
public bool Equals(Book a, Book b)
{
if (Object.ReferenceEquals(a, b)) return true;
return a != null && b != null && a.Title.Equals(b.Title) && a.Author.Equals(b.Author);
}
public int GetHashCode(Book obj)
{
int hashBookTitle = obj.Title == null ? 0 : obj.Title.GetHashCode();
int hashBookAuthor = obj.Author == null ? 0 : obj.Author.GetHashCode();
return hashBookTitle ^ hashBookAuthor;
}
}
As you can see,
Id
property is ignored. Only
Title
and
Author
of book are compared.
Now, sample xml data, which we'll use in next step:
<books>
<book id="1">
<author>James Booldog</author>
<title>The honor </title>
</book>
<book id="2">
<author>Joanne Clipboard</author>
<title>Paranoya</title>
</book>
<book id="3">
<author>Jamie Douplo</author>
<title>Lego star</title>
</book>
</books>
Usage:
void Main()
{
string x1 = @"<?xml version='1.0' encoding='utf-8'?>
<books>
<book id='1'>
<author>James Booldog</author>
<title>The honor </title>
</book>
<book id='2'>
<author>Joanne Clipboard</author>
<title>Paranoya</title>
</book>
<book id='3'>
<author>Jamie Douplo</author>
<title>Lego star</title>
</book>
</books>";
string x2 = @"<?xml version='1.0' encoding='utf-8'?>
<books>
<book id='1'>
<author>James Booldog</author>
<title>The honor </title>
</book>
<book id='2'>
<author>Joanne Clipboard</author>
<title>Paranoya</title>
</book>
<book id='3'>
</book>
</books>";
XDocument doc1 = XDocument.Parse(x1);
XDocument doc2 = XDocument.Parse(x2);
List<Book> bks1 = doc1.Descendants("book")
.Select(x=>new Book
(
(int)x.Attribute("id"),
(string)x.Element("author"),
(string)x.Element("title")
)).ToList<Book>();
var bks2 = doc2.Descendants("book")
.Select(x=>new Book
(
(int)x.Attribute("id"),
(string)x.Element("author"),
(string)x.Element("title")
));
var missingBooks1 = bks1.Except(bks2, new BookComparer());
}
In case when you use custom collection class:
public class Books : CollectionBase
{
public void Add(Book b)
{
List.Add(b);
}
public void AddRange(List<Book> books)
{
foreach(Book b in books)
{
List.Add(b);
}
}
public void AddRange(IEnumerable<Book> books)
{
foreach(var b in books)
{
List.Add((Book)b);
}
}
public void Remove(int index)
{
if(index<0 || index>List.Count-1)
{
throw new IndexOutOfRangeException("Invalid index!");
}
else
{
List.Remove(index);
}
}
}
a comparison might be achieved this way:
Books books1 = new Books();
books1.AddRange(bks1);
Books books2 = new Books();
books2.AddRange(bks2);
var missingBooks2 = books1.Cast<Book>().Select(a=>a).Except(books2.Cast<Book>().Select(b=>b), new BookComparer());
Final note: This is very basic example. I hope you get an idea.
Try to implement your own method to compare xml data!