I have a XML structure like the following:
[...]
<Fruits>
<Fruit>
<Specification>id_1001_0</Specification>
<Weight>23</Weight>
</Fruit>
</Fruits>
<FruitSpecification id="id_1001_0">
<Type>Apple</Type>
</FruitSpecification>
[...]
I want to use Linq to XML to read this into (non-anonymous) objects.
Let´s say i have the following code:
var fruits = from xele in xDoc.Root.Element("Fruits").Elements("Fruit")
select new Fruit()
{
Weight = xele.Element("Weight").Value
}
How can i extend the query to join the correct FruitSpecification tag?
The goal would be to be able to write this:
var fruits = from xele in xDoc.Root.Element("Fruits").Elements("Fruit")
//some join?
select new Fruit()
{
Weight = xele.Element("Weight").Value,
Type = xjoinedelement.Element("Type").Value
}
I hope this is understandable, i made this "fruit" sample up, my actual XML is much more complicated...
Yes, simple join will do the trick:
var fruits =
from f in xdoc.Root.Element("Fruits").Elements("Fruit")
join fs in xdoc.Root.Elements("FruitSpecification")
on (string)f.Element("Specification") equals (string)fs.Attribute("id")
select new Fruit()
{
Weight = (int)f.Element("Weight"),
Type = (string)fs.Element("Type")
};
Fruit class definition:
public class Fruit
{
public int Weight { get; set; }
public string Type { get; set; }
}
Try this:
class Fruit
{
public int Weight { get; set; }
public string Type { get; set; }
}
string xml = #"
<root>
<Fruits>
<Fruit>
<Specification>id_1001_0</Specification>
<Weight>23</Weight>
</Fruit>
<Fruit>
<Specification>id_1002_0</Specification>
<Weight>25</Weight>
</Fruit>
</Fruits>
<FruitSpecification id='id_1001_0'>
<Type>Apple</Type>
</FruitSpecification>
<FruitSpecification id='id_1002_0'>
<Type>Orange</Type>
</FruitSpecification>
</root>";
XElement element = XElement.Parse(xml);
IEnumerable<XElement> xFruites = element.Descendants("Fruit");
IEnumerable<XElement> xFruitSpecifications = element.Descendants("FruitSpecification");
IEnumerable<Fruit> frits = xFruites.Join(
xFruitSpecifications,
f => f.Element("Specification").Value,
s => s.Attribute("id").Value,
(f, s) =>
new Fruit
{
Type = s.Element("Type").Value,
Weight = int.Parse(f.Element("Weight").Value)
});
Related
I have an xml file as below:
<Message xsi:schemaLocation ="..">
<Header>..</Header>
<Body>
<History
xmlns="..">
<Number></Number>
<Name></Name>
<Item>
<CreateDate>..</CreateDate>
<Type>..</Type>
<Description></Description>
</Item>
<Item>
<CreateDate>..</CreateDate>
<Type>..</Type>
<Description>1..</Description>
<Description>2..</Description>
</Item>
</History>
</Body>
</Message>
I would like to create and object from this as History object.
public class History
{
public string Name { get; set; }
public string Number { get; set; }
public List<Item> Items { get; set; }
}
var xElement = XDocument.Parse(xmlString);
XElement body = (XElement)xElement.Root.LastNode;
XElement historyElement = (XElement)body.LastNode;
var history = new History
{
Name = (string)historyElement.Element("Name"),
Number = (string)historyElement.Element("Number"),
Items = (
from e in historyElement.Elements("Item")
select new Item
{
CraeteDate = DateTime.Parse(e.Element("CreateDate").Value),
Type = (string)e.Element("Type").Value,
Description = string.Join(",",
from p in e.Elements("Description") select (string)p.Element("Description"))
}).ToList()
};
Why this does not work?
The values are always null.
It seems that "historyElement.Element("Name")" is always null even there is an element and value for the element.
Any idea what am I missing?
Thanks
It's due to the namespace, try doing this:
XNamespace ns = "http://schemas.microsoft.com/search/local/ws/rest/v1";// the namespace you have in the history element
var xElement = XDocument.Parse(xmlString);
var history= xElement.Descendants(ns+"History")
.Select(historyElement=>new History{ Name = (string)historyElement.Element(ns+"Name"),
Number = (string)historyElement.Element(ns+"Number"),
Items = (from e in historyElement.Elements(ns+"Item")
select new Item
{
CraeteDate= DateTime.Parse(e.Element(ns+"CreateDate").Value),
Type = (string) e.Element(ns+"Type").Value,
Description= string.Join(",",
from p in e.Elements(ns+"Description") select (string)p)
}).ToList()
}).FirstOrDefault();
If you want to read more about this subject, take a look this link
A couple of minor things here, the xml was malformed here. So had to take a while to test and make it work.
You have an xsi in the front which I assume should be somewhere mentioned in the xsd.
Turns out you have to append the namespace if your xml node has a namespace attached to it as you are parsing the xml tree here:
My sample solution looked like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace ConsoleApplication1
{
public class History
{
public string Name { get; set; }
public string Number { get; set; }
public List<Item> Items { get; set; }
}
public class Item
{
public DateTime? CreateDate { get; set; }
public string Type { get; set; }
public string Description { get; set; }
}
class Program
{
static void Main(string[] args)
{
string xmlString =
#"<Message>
<Header>..</Header>
<Body>
<History
xmlns=""http://schemas.somewhere.com/types/history"">
<Number>12</Number>
<Name>History Name</Name>
<Item>
<CreateDate></CreateDate>
<Type>Item 1 Type</Type>
<Description>Item 1 Description</Description>
</Item>
<Item>
<CreateDate></CreateDate>
<Type>Item 2 Type</Type>
<Description>Item 2 Description 1</Description>
<Description>Item 2 Description 2</Description>
</Item>
</History>
</Body>
</Message>";
XNamespace ns = "http://schemas.somewhere.com/types/history";
var xElement = XDocument.Parse(xmlString);
var historyObject = xElement.Descendants(ns +"History")
.Select(historyElement => new History
{
Name = historyElement.Element(ns + "Name")?.Value,
Number = historyElement.Element(ns + "Number")?.Value,
Items = historyElement.Elements(ns + "Item").Select(x => new Item()
{
CreateDate = DateTime.Parse(x.Element(ns + "CreateDate")?.Value),
Type = x.Element(ns + "Type")?.Value,
Description = string.Join(",", x.Elements(ns + "Description").Select(elem=>elem.Value))
}).ToList()
}).FirstOrDefault();
}
}
}
If you dont wan't to care about finding the namespace, you might want to try the following:
var document = XDocument.Parse(xmlString);
var historyObject2 = document.Root.Descendants()
.Where(x=>x.Name.LocalName == "History")
.Select(historyElement => new History
{
Name = historyElement.Element(historyElement.Name.Namespace + "Name")?.Value,
Number = historyElement.Element(historyElement.Name.Namespace+ "Number")?.Value,
Items = historyElement.Elements(historyElement.Name.Namespace + "Item").Select(x => new Item()
{
//CreateDate = DateTime.Parse(x.Element("CreateDate")?.Value),
Type = x.Element(historyElement.Name.Namespace + "Type")?.Value,
Description = string.Join(",", x.Elements(historyElement.Name.Namespace + "Description").Select(elem => elem.Value))
}).ToList()
}).FirstOrDefault();
At present i am fetching data from xml file using LINQ but the problem is i am using XDocument to load xml file but XDocument class load xml data into memory. so if there is 10,000 data in my xml file then XDocument class will load 10,000 data into memory. so some one tell me if use read xml data with XmlReader class then it will not dump full data into memory.
At present this way i am fetching data from xml file.
My xml data look like:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Orders>
<OrderID>10248</OrderID>
<CustomerID>VINET</CustomerID>
<EmployeeID>5</EmployeeID>
<OrderDate>1996-07-04T00:00:00</OrderDate>
<RequiredDate>1996-08-01T00:00:00</RequiredDate>
<ShippedDate>1996-07-16T00:00:00</ShippedDate>
<ShipVia>3</ShipVia>
<Freight>32.3800</Freight>
<ShipName>Vins et alcools Chevalier</ShipName>
<ShipAddress>59 rue de l'Abbaye</ShipAddress>
<ShipCity>Reims</ShipCity>
<ShipPostalCode>51100</ShipPostalCode>
<ShipCountry>France</ShipCountry>
</Orders>
</Root>
here i am posting code wich fetch data from xml file with order by and paging.
XDocument document = XDocument.Load(#"c:\users\documents\visual studio 2010\Projects\WindowsFormsApplication5\WindowsFormsApplication5\Orders.xml");
bool isDesc = true;
//setup basic query
var query = from r in document.Descendants("Orders")
select new
{
OrderID = r.Element("OrderID").Value,
CustomerID = r.Element("CustomerID").Value,
EmployeeID = r.Element("EmployeeID").Value,
};
//setup query result ordering,
//assume we have variable to determine ordering mode : bool isDesc = true/false
if (isDesc)
query = query.OrderByDescending(o => o.OrderID);
else
query = query.OrderBy(o => o.OrderID);
//setup pagination,
//f.e displaying result for page 2 where each page displays 100 data
var page = 1;
var pageSize = 5;
query = query.Skip(page - 1 * pageSize).Take(pageSize);
//execute the query to get the actual result
//var items = query.ToList();
dataGridView1.DataSource = query.ToList();
So some one tell me how could i use xmlreader to read data from xml file with pagination and order by clause will be there.
I got one hit but do not understand how to use it for my purpose:
using( var reader = XmlReader.Create( . . . ) )
{
reader.MoveToContent();
reader.ReadToDescendant( "book" );
// skip N <book> elements
for( int i = 0; i < N; ++i )
{
reader.Skip();
reader.ReadToNextSibling( "book" );
}
// read M <book> elements
for( int i = 0; i < M; ++i )
{
var s = reader.ReadOuterXml();
Console.WriteLine( s );
reader.ReadToNextSibling( "book" );
}
}
So please see the above code and help me to construct the code which would use xml reader to fetch paginated data.
Try this code. Your latest posting is locked so I had to answer here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
Order order = new Order();
while(reader.ReadToFollowing("Orders"))
{
string xml = reader.ReadOuterXml().Trim();
XElement element = XElement.Parse(xml);
order.Add(element);
}
order.Sort();
}
}
public class Order
{
public void Add(XElement element)
{
if (orders == null)
{
orders = new List<Order>();
}
Order newOrder = new Order();
newOrder.OrderId = int.Parse(element.Element("OrderID").Value);
newOrder.CustomerId = element.Element("CustomerID").Value;
newOrder.EmployeeId = int.Parse(element.Element("EmployeeID").Value);
newOrder.OrderDate = DateTime.Parse(element.Element("OrderDate").Value);
newOrder.RequiredDate = DateTime.Parse(element.Element("RequiredDate").Value);
newOrder.ShippedDate = DateTime.Parse(element.Element("ShippedDate").Value);
newOrder.ShipVia = int.Parse(element.Element("ShipVia").Value);
newOrder.Freight = double.Parse(element.Element("Freight").Value);
newOrder.ShipName = element.Element("ShipName").Value;
newOrder.ShipAddress = element.Element("ShipAddress").Value;
newOrder.ShipCity = element.Element("ShipCity").Value;
newOrder.ShipPostalCode = int.Parse(element.Element("ShipPostalCode").Value);
newOrder.ShipCountry = element.Element("ShipCountry").Value;
orders.Add(newOrder);
}
public void Sort()
{
orders = orders.OrderBy(x => x.OrderId).ToList();
}
public static List<Order> orders { get; set; }
public int OrderId { get; set; }
public string CustomerId { get; set; }
public int EmployeeId { get; set; }
public DateTime OrderDate { get; set; }
public DateTime RequiredDate { get; set; }
public DateTime ShippedDate { get; set; }
public int ShipVia { get; set; }
public double Freight { get; set; }
public string ShipName { get; set; }
public string ShipAddress { get; set; }
public string ShipCity { get; set; }
public int ShipPostalCode { get; set; }
public string ShipCountry { get; set; }
}
}
I have the following XML excerpt. I have no problem extracting the first step of XML but I can't figure out how to get to the second layer and extract each layer. Specifically, the user info in XML below.
Any help will be appreciated...
<?xml version="1.0" encoding="UTF-8" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getStatusReportResponse xmlns:ns2="http://xxxxxxxxx.com/">
<return>
<statusReport>
<record>
<assessorDate />
<assessorOffice />
<availableDate />
<awardDate>01/01/2014</awardDate>
<awardValue>1000000</awardValue>
<businessSector>SYSTEMS</businessSector>
<user>
<accessGrantedDate />
<emailAddress>john.usda#noemail.mil</emailAddress>
<name>JOHN USDA</name>
<phoneNumber>XXX-XXX-XXXX</phoneNumber>
<role>Focal Point</role>
</user>
<user>
<accessGrantedDate />
<emailAddress>john.usda#noemail.mil</emailAddress>
<name>JOHN USDA</name>
<phoneNumber>XXX-XXX-XXXX</phoneNumber>
<role>Focal Point</role>
</user>
</record>
</statusReport>
</return>
</ns2:getStatusReportResponse>
</S:Body>
</S:Envelope>
I've tried this but it only get's me a list of the first user record and not all of them.
var records = from x in xml.Descendants("record")
select new
{
awardDate = (string) x.Descendants("awardDate").FirstOrDefault().Value
,userList = (List<string>) x.Descendants("user").Elements() //.Elements("accessGrantedDate")
.Select(a => a.Value).ToList()
};
I am assuming this is the model of your User class:
public class User
{
public DateTime? AccessGrantedDate { get; set; }
public string EMailAddress { get; set; }
public string Name { get; set; }
public string PhoneNumber { get; set; }
public string Role { get; set; }
}
And this is extracting the User class from the XML:
var records = from x in xml.Descendants("record")
select new
{
AwardDate = (string)x.Element("awardDate"),
UserList = x.Descendants("user").Select(user => new User
{
AccessGrantedDate = string.IsNullOrEmpty((string)user.Element("accessGrantedDate")) ?
(DateTime?) null : DateTime.Parse((string)user.Element("accessGrantedDate")),
EMailAddress = (string)user.Element("emailAddress"),
Name = (string)user.Element("name"),
PhoneNumber = (string)user.Element("phoneNumber"),
Role = (string)user.Element("role")
})
};
As far as I can tell, your code gets the data of all users in the file, but only the values of nodes.
The more organized way to do this would be:
class User
{
public string accessGrantedDate { get; set; }
public string emailAddress { get; set; }
public string name { get; set; }
public string phoneNumber { get; set; }
public string role { get; set; }
}
then:
var records = from x in xml.Descendants("record")
select new
{
awardDate = (string) x.Descendants("awardDate").FirstOrDefault().Value
,userList = x.Descendants("user").Select(a=>new User
{
accessGrantedDate= a.Element("accessGrantedDate").Value,
emailAddress=a.Element("emailAddress").Value,
name=a.Element("name").Value,
phoneNumber=a.Element("phoneNumber").Value,
role = a.Element("role").Value
}).ToList()
};
or, if you don't want to organize your data this way, you may use a list of lists:
var records = from x in xml.Descendants("record")
select new
{
awardDate = (string)x.Descendants("awardDate").FirstOrDefault().Value,
userList = x.Descendants("user").Select(a => a.Elements().Select(b=>b.Value).ToList()).ToList()
};
I solved it by using a 2nd query for detail records, therefore, creating a List as a object inside the outer list.
var records = from x in xml.Descendants("record")
select new
{
awardDate = x.Element("awardDate")
,
userList = from u in x.Descendants("user")
select new
{
accessGrantedDate = (string) u.Element("accessGrantedDate")
,emailAddress =(string) u.Element("emailAddress"
,name = (string) u.Element("name")
,phoneNumber = (string) u.Element("phoneNumber")
,role = (string) u.Element("role")
}
};
I hope you guys will be able to help me out?
I have this XML structure:
<DataBase
xsi:schemaLocation="http://somestuff.new/xml http://somestuff.xsd"
xmlns:ns3="http://somestuff.new/ns3"
xmlns:ns2="http://somestuff.new/ns2"
xmlns="http://somestuff.new/ns"
xmlns:xsi="http://somestuff.new/XMLScema-instance"
xmlns:ns4="http://somestuff.new/ns4">
<Cars>
<SmallCars attribute="Something">
<Id>licenceplate</Id>
<Parts attribute="All Parts">
<Extras>
<Gauges xmlns="http://somestuff.new/ns3">
<Speed>100</Speed>
<Rpm>3200</Rpm>
</Gauges>
</Extras>
</Parts>
</SmallCars>
</Cars>
</DataBase>
I have then created a class of Cars like this:
public class Cars
{
public string CarType { get; set; }
public List<Parts> CarParts { get; set; }
}
And a Class of Parts:
public class Parts
{
public List<Gauges> CarExtras { get; set; }
}
And last but not least a class of Gauges:
public class Gauges
{
public double Speed { get; set; }
public int Rpm { get; set; }
}
Now I would like to create a LINQ query that gets the values from the Gauges part of the XML but I seem to fail in my attempt:
XDocument xml = XDocument.Load(file);
XNamespace xmlns =XNamespace.Get("http://somestuff.new/ns");
XNamespace ns3 = XNamespace.Get("http://somestuff.new/ns3");
var Cars = from car in xml.Descendants(ns + "Cars")
select new Cars
{
CarParts = (from part in car.Descendants(ns + "Parts")
select new Parts
{
CarExtras = (from extra in part.Descendants(ns + "Extras")
select new Gauges
{
Speed = (double?)extra.Element(ns3 + "Speed") ?? 0.0,
Rpm = (int?)extra.Element(ns3 + "Rpm") ?? 0
})
})
});
I have tried a lot of combinations with the namespaces because it changes when I get to Gauges but I do not get any values returned.
Hope someone can help me out here?
Notice that extra in in your linq-to-xml code is <Extras> element, and since <Speed> and <Rpm> are not direct child of <Extras> you can't select any of them by using extra.Element(ns3 + "elementName"). You can use Descendants instead of Element in this case :
XNamespace ns =XNamespace.Get("http://somestuff.new/ns");
XNamespace ns3 = XNamespace.Get("http://somestuff.new/ns3");
var Cars = from car in xml.Descendants(ns + "Cars")
select new Cars
{
CarParts = (from part in car.Descendants(ns + "Parts")
select new Parts
{
CarExtras =
(from extra in part.Descendants(ns + "Extras")
select new Gauges
{
Speed =
(double?)
extra.Descendants(ns3 + "Speed").FirstOrDefault() ??
0.0,
Rpm =
(int?)
extra.Descendants(ns3 + "Rpm").FirstOrDefault() ?? 0
}).ToList()
}).ToList()
};
I am trying to do a XML parser which will extract data from a website using REST service, the protocol for communication is HTTP, the data I get is in XML format, and I get to the data I need after several requests to different addresses on the server. I need to parse this data to c# objects so I can operate with them lately.
The information on the server is on 5 levels( I am willing to make work only 4 of them for know)
1-List of vendors
2-List of groups
3-List of subgroups
4-List of products
5-List of full information about products
After I get to 4th level I need to check if the product is in my database or if it has different details so I can add or update it.
With "GET" request to a server I get XML with this structure:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<vendors>
<vendor>
<id>someID</id>
<name>someName</name>
</vendor>
<vendor>
<id>someId1</id>
<name>somename1</name>
</vendor>
</vendors>
XML structure for groups is the same :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<groups vendor_id="43153185318">
<group>
<id>someID</id>
<name>someName</name>
</group>
<group>
<id>someId1</id>
<name>somename1</name>
</group>
The XML structure is analogical for subgroups and products, except that for products I have more elements like catalog_num, price etc.
I made the classes as follows :
public class VendorList
{
public List<Vendor> vendor_list { get; set; }
public VendorList()
{
vendor_list = new List<Vendor>();
}
}
public class Vendor
{
public string id { get; set; }
public string name { get; set; }
public List<Group> groups_list { get; set; }
public Vendor()
{
id = "N/A";
name = "N/A";
groups_list = new List<Group>();
}
}
public class Group
{
public string id { get; set; }
public string name { get; set; }
public List<SubGroup> subgroup_list { get; set; }
public Group()
{
id = "N/A";
name = "N/A";
subgroup_list = new List<SubGroup>();
}
}
public class SubGroup
{
public string id { get; set; }
public string name { get; set; }
public List<Product> product_list { get; set; }
public SubGroup()
{
id = "N/A";
name = "N/A";
product_list = new List<Product>();
}
}
public class Product
{
public string available { get; set; }
public string catalog_num { get; set; }
public string code { get; set; }
public string currency { get; set; }
public string description { get; set; }
public string haracteristics { get; set; }
public string product_id { get; set; }
public string model { get; set; }
public string name { get; set; }
public string price { get; set; }
public string price_dds { get; set; }
public string picture_url { get; set; }
public Product()
{
available = "N/A";
catalog_num = "N/A";
code = "N/A";
currency = "N/A";
description = "N/A";
haracteristics = "N/A";
product_id = "N/A";
model = "N/A";
name = "N/A";
price = "N/A";
price_dds = "N/A";
picture_url = "N/A";
}
}
and the Parser method like this :
public static void FillVendor(string url)
{
string result = GetXMLstream(url);
var vendors = new VendorList();
XmlDocument doc = new XmlDocument();
doc.Load(new StringReader(result));
doc.Save(#"D:/proba/proba.xml");
XDocument d = XDocument.Load(#"D:/proba/proba.xml");
vendors.vendor_list = (from c in d.Descendants("vendor")
select new Vendor()
{
id = c.Element("id").Value,
name = c.Element("name").Value
}).ToList<Vendor>();
foreach (Vendor v in vendors.vendor_list)
{
FillGroups(v.id);
}
}
public static void FillGroups(string vendorID)
{
string url = "main address" + vendorID;
string result = GetXMLstream(url);
var group = new Vendor();
XmlDocument doc = new XmlDocument();
doc.Load(new StringReader(result));
doc.Save(#"D:/proba/proba1.xml");
XDocument d = XDocument.Load(#"D:/proba/proba1.xml");
group.groups_list = (from g in d.Descendants("group")
select new Group()
{
id = g.Element("id").Value,
name = g.Element("name").Value
}).ToList<Group>();
foreach (Group g in group.groups_list)
{
FillSubGroup(vendorID, g.id);
}
}
public static void FillSubGroup(string vendorID, string groupID)
{
string url = "main address" + vendorID+"/"+groupID;
string result = GetXMLstream(url);
var subgroup = new Group();
XmlDocument doc = new XmlDocument();
doc.Load(new StringReader(result));
doc.Save(#"D:/proba/proba2.xml");
XDocument d = XDocument.Load(#"D:/proba/proba2.xml");
subgroup.subgroup_list = (from g in d.Descendants("subgroup")
select new SubGroup()
{
id = g.Element("id").Value,
name = g.Element("name").Value
}).ToList<SubGroup>();
foreach (SubGroup sb in subgroup.subgroup_list)
{
FillProduct(vendorID, groupID, sb.id);
}
}
public static void FillProduct(string vendorID,string groupID,string subgroupID)
{
string url = "main address" + vendorID + "/" + groupID+"/"+subgroupID;
string result = GetXMLstream(url);
var product = new SubGroup();
XmlDocument doc = new XmlDocument();
doc.Load(new StringReader(result));
doc.Save(#"D:/proba/proba2.xml");
XDocument d = XDocument.Load(#"D:/proba/proba2.xml");
product.product_list = (from g in d.Descendants("subgroup")
select new Product()
{
available = g.Element("available").Value,
catalog_num = g.Element("catalog_num").Value,
code = g.Element("code").Value,
currency = g.Element("currency").Value,
description = g.Element("description").Value,
haracteristics = g.Element("haracteristics").Value,
product_id = g.Element("id").Value,
model = g.Element("model").Value,
name = g.Element("name").Value,
price = g.Element("price").Value,
price_dds = g.Element("price_dds").Value,
picture_url = g.Element("small_picture").Value,
}).ToList<Product>();
}
But after finishing parsing I try to check if my Lists are populated with objects, but I get an error which says that they are null "NullReferenceException"
So my question is did I make classes properly and is my parsing method right ( you can suggest how to parse the xml without creating a file on my computer) and if I didn't where is my mistake and how should I make it work properly?
Thanks in advance!
modify this line add 's'( vendor -> vendors)
-> vendors.vendor_list = (from c in d.Descendants("vendor")
and the same case for group -> groups
Instead of making the classes yourself.
Create a properly formatted XML Schema either manually or with Visual Studio and then from that XSD File you've created generate your C# Classes.