I have the following:
XDocument xdoc = XDocument.Load("C:\\myfile.xml");
List<Tag> list = new List<Tag>();
foreach (var tag in xdoc.Descendants("META"))
{
string name = tag.Attribute("name").Value;
string value = tag.Element("value").Value;
list.Add(new Tag { Name = name, Value = value, Score = score, Key = key });
}
but I need to only get the META elements under the element ARTICLE.
Can I add this to the linq query somehow?
The xml looks similar to this:
<DOCUMENT>
<META name="aaa"/>
<ARTICLE>
<META name="bbb" value="708" score="0.58" key="6008"/>
</ARTICLE>
</DOCUMENT>
Thanks for any suggestions
In the end I need to do something like the following:
XDocument xdoc = XDocument.Load(tr);
var meta = from el in xdoc.Descendants("ARTICLE").Elements("META")
where (string) el.Attribute("name") == "RAW"
select el;
List<Tag> list = new List<Tag>();
foreach (var tag in meta)
{
string name = tag.Attribute("name").Value;
string value = tag.Attribute("value").Value;
string score = tag.Attribute("score").Value;
string key = tag.Attribute("key").Value;
list.Add(new Tag { Name = name, Value = value, Score = score, Key = key });
}
The reason for this is that I needed to match the attribute where the name was equal to RAW.
Please correct me if there's a better way to do this!
To find them anywhere in the document, use xdoc.Descendants("ARTICLE") to find all the ARTICLE elements, and then Elements("META") from there to find all the direct META children elements.
In addition, you can perform the projection and the conversion to a list in the same query:
var list = xdoc.Descendants("ARTICLE")
.Elements("META")
.Select(x => new Tag { Name = (string) x.Attribute("name"),
Value = (string) x.Attribute("value"),
Key = key })
.ToList();
Related
i have a collection of service users, i want to iterate over ServiceUsers and extract value from ServiceUser, (ID, USER_NAME, UN_ID, IP, NAME)
<ServiceUsers xmlns="">
<ServiceUser>
<ID>280334</ID>
<USER_NAME>YVELAMGAMOIYENET12:206322102</USER_NAME>
<UN_ID>731937</UN_ID>
<IP>91.151.136.178</IP>
<NAME>?????????????????????: 123456</NAME>
</ServiceUser>
<ServiceUser>
<ID>266070</ID>
<USER_NAME>ACHIBALANCE:206322102</USER_NAME>
<UN_ID>731937</UN_ID>
<IP>185.139.56.37</IP>
<NAME>123456</NAME>
</ServiceUser>
</ServiceUsers>
my Code looks like this, but i am getting null point exception.
XDocument doc = XDocument.Parse(xml)
List<XElement> xElementList = doc.Element("ServiceUsers").Descendants().ToList();
foreach (XElement element in xElementList)
{
string TEST= element.Element("Name").Value;
comboBoxServiceUser.Items.Add(element.Element("Name").Value);
}
I used the example from XmlSerializer.Deserialize Method as the base for the following snippet that reads the provided xml.
var serializer = new XmlSerializer(typeof(ServiceUsers));
ServiceUsers i;
using (TextReader reader = new StringReader(xml))
{
i = (ServiceUsers)serializer.Deserialize(reader);
}
[XmlRoot(ElementName = "ServiceUsers")]
public class ServiceUsers : List<ServiceUser>
{
}
public class ServiceUser
{
[XmlElement(ElementName = "ID")]
public string Id {get; set;}
}
I think the basis of the problem is your trailing 's' to put it short, You iterate ServiceUser not ServiceUsers
Anyway this runs through fine:
[Fact]
public void CheckIteratorTest()
{
var a = Assembly.GetExecutingAssembly();
string[] resourceNames = a.GetManifestResourceNames();
string nameOf = resourceNames.FirstOrDefault(x => x.Contains("SomeXml"));
Assert.NotNull(nameOf);
using var stream = a.GetManifestResourceStream(nameOf);
Assert.NotNull(stream);
var reader = new StreamReader(stream, Encoding.UTF8);
var serialized = reader.ReadToEnd();
var doc = XDocument.Parse(serialized);
var elemList = doc.Root.Elements("ServiceUser").ToList();
Assert.NotEqual(0, elemList.Count);
foreach(var serviceUser in elemList)
{
System.Diagnostics.Debug.WriteLine($"Name : {serviceUser.Name ?? "n.a."}");
}
}
As being said: XML is case-sensitive. Next issue is .Descendants() returns all the descendant nodes, nested ones, etc, 12 nodes in this case. So NullPointerException will happen even if you fix a "typo".
Here is your fixed code:
XDocument doc = XDocument.Parse(xml);
var xElementList = doc
.Element("ServiceUsers") // picking needed root node from document
.Elements("ServiceUser") // picking exact ServiceUser nodes
.Elements("NAME") // picking actual NAME nodes
.ToList();
foreach (XElement element in xElementList)
{
var TEST = element.Value;
Console.WriteLine(TEST); // do what you were doing instead of console
}
Use doc.Element("ServiceUsers").Elements() to get the<ServiceUser> elements. Then you can loop over the child values of those in a nested loop.
var doc = XDocument.Parse(xml);
foreach (XElement serviceUser in doc.Element("ServiceUsers").Elements()) {
foreach (XElement element in serviceUser.Elements()) {
Console.WriteLine($"{element.Name} = {element.Value}");
}
Console.WriteLine("---");
}
Prints:
ID = 280334
USER_NAME = YVELAMGAMOIYENET12:206322102
UN_ID = 731937
IP = 91.151.136.178
NAME = ?????????????????????: 123456
---
ID = 266070
USER_NAME = ACHIBALANCE:206322102
UN_ID = 731937
IP = 185.139.56.37
NAME = 123456
---
Note: Elements() gets the (immediate) child elements where as Descendants() returns all descendants. Using Elements() gives you a better control and allows you to get the properties grouped by user.
You can also get a specific property like this serviceUser.Element("USER_NAME").Value. Note that the tag names are case sensitive!
I'm trying to get some data from an XML file - see below.
Basically for each session data, I get all the elements in it and store it but I need to get the movie_Name from the elements with reference by the movie.
<Schedule_Data>
<Movies>
<Movie>
<Cinema_ID>3169101</Cinema_ID>
<Movie_ID>1012689</Movie_ID>
<Movie_Name>2D Captain America: Civil War</Movie_Name>
<Rating>PG13</Rating>
<Runtime>160</Runtime>
</Movie>
<Movie>
<Cinema_ID>3169101</Cinema_ID>
<Movie_ID>1012984</Movie_ID>
<Movie_Name>2D Zootopia</Movie_Name>
<Rating>PG</Rating>
<Runtime>115</Runtime>
</Movie>
</Movies>
<Sessions>
<Session>
<Cinema_ID>8888888</Cinema_ID>
<Movie_ID>1012689</Movie_ID>
<Session_ID>1083592422</Session_ID>
<Price_group_code>10007</Price_group_code>
<Auditorium_Number>9</Auditorium_Number>
<Assigned_Seating>True</Assigned_Seating>
<Attribute></Attribute>
<Date_time>20160607141000</Date_time>
<Total_Seats>87</Total_Seats>
<Available_Seats>87</Available_Seats>
</Session>
<Session>
<Cinema_ID>8888888</Cinema_ID>
<Movie_ID>1012984</Movie_ID>
<Session_ID>1083592423</Session_ID>
<Price_group_code>10007</Price_group_code>
<Auditorium_Number>9</Auditorium_Number>
</Session>
</Sessions>
</Schedule_Data>
Currently my code is:
XDocument thisXML = XDocument.Parse(responseText);
//get the dates element (which contains all the date nodes)
XElement datesElement = thisXML.Element("Schedule_Data").Element("Sessions");
//use linq to compile an ienumerable of the date nodes
var dates = from dateNode in datesElement.Elements("Session")
select dateNode;
//get the dates element (which contains all the film nodes)
XElement MoviesElement = thisXML.Element("Schedule_Data").Element("Movies");
foreach (XElement session in dates)
{
//get movie name
var films = from filmnode in MoviesElement.Elements("Movie")
select filmnode;
var movieId = session.Element("Movie_ID").Value;
// This is where i try do the where clause and try get the value but it returns null
var answer = from reply in films
where reply.Element("Movie_ID").Value == movieId
select films.Elements("Movie_Name");
//create a new session import record
ORM.Sessionimportattemptimportedsession newSessionimportattemptimportedsession = new ORM.Sessionimportattemptimportedsession();
//check data and set properties
newSessionimportattemptimportedsession.MovieTitle = answer.ToString();
newSessionimportattemptimportedsession.ScreenNumber = session.Element("Screen_bytNum").Value;
.....
numberOfSessions++;
}
Any suggestions?
You're just performing a join between sessions and movies. Just do this:
var query =
from s in doc.Descendants("Session")
join m in doc.Descendants("Movie")
on (string)s.Element("Movie_ID") equals (string)m.Element("Movie_ID")
select new
{
MovieName = (string)m.Element("Movie_Name"),
Session = s,
Movie = m,
};
it could be a silly question, but i want ta ask, how can I save the same element from an .XML file with different content as an array.
Example XML:
<ithem>
<description>description1</description>
<description>description2</description>
<description>description3</description>
</ithem>
then string [] descriptions will be
descriptions[0] = "description1";
descriptions[1] = "description2";
descriptions[2] = "description3";
Please help!
Using LINQ to XML it would be:
XElement root = XElement.Load(xmlFile);
string[] descriptions = root.Descendants("description").Select(e => e.Value).ToArray();
or
string[] descriptions = root.Element("ithem").Elements("description").Select(e => e.Value).ToArray();
Use XmlDocument to parse the XML :
var map = new XmlDocument();
map.Load("path_to_xml_file"); // you can also load it directly from a string
var descriptions = new List<string>();
var nodes = map.DocumentElement.SelectNodes("ithem/description");
foreach (XmlNode node in nodes)
{
var description = Convert.ToString(node.Value);
descriptions.Add(description);
}
And you get it as an array from:
descriptions.ToArray();
I am quite new to Linq to XML & trying to Parse a xml string & retrieve its attribute Value using Linq to XML in C#.
My XML string looks like :
<configuration xmlns:lui="http://www.xyz.com/UITags">
<pub id="pubId1" lang="en-US">
<configitem name="visible" value="visible"/>
<configitem name="working_status" value="unlocked"/>
<configitem name="prepared" value="prepared"/>
</pub>
.....
.....
<pub id="Pub2" lang="es-XM">...</pub>
....
....
</configuration>
I want to fetch the value of 'id' & 'lang' from pub node & value of attribute named 'working_status' from configitem Node.
Now as I am getting the above xml as a string parameter (i.e. myXmlData), by doing
XmlDocument doc = new XmlDocument();
doc.LoadXml(myXmlData);
XmlNodeList publicationsNodeList = doc.SelectNodes("//configuration/pub");
...
...
Then I have to loop through using foreach, which I want to avoid as much as possible.
Can anyone help me how to achieve this using Linq to XML in C#, rather then conventional way.
Following LINQ to XML query will return sequence of anonymous objects with id, lang, and working status of pub elements:
var xdoc = XDocument.Parse(myXmlData);
var query =
from p in xdoc.Root.Elements("pub")
let ws = p.Elements("configitem")
.FirstOrDefault(c => (string)c.Attribute("name") == "working_status")
select new {
Id = (string)p.Attribute("id"),
Lang = (string)p.Attribute("lang"),
WorkingStatus = (ws == null) ? null : (string)ws.Attribute("value")
};
For your sample xml it returns two objects with following data:
{
Id = "pubId1",
Lang = "en-US",
WorkingStatus = "unlocked"
},
{
Id = "Pub2",
Lang = "es-XM",
WorkingStatus = null
}
var query = from x in xdoc.Descendants("pub")
select new
{
Id = (string)x.Attribute("id"),
Lang = (string)x.Attribute("lang"),
Name = x.Descendants("configitem").Select(y => y.Attribute("name").Value).FirstOrDefault(y => y == "working_status")
};
I do have a XML similar like this
<?xml version="1.0" encoding="UTF-8"?>
<e_schema>
<schema_name value="shema1">
<contact>
<id>1</id>
<firstName>firstname1</firstName>
<lastName>lastname1</lastName>
<department>IT</department>
<emailAddress>lastname1#mydomain.com</emailAddress>
<lineManagerId>22331470</lineManagerId>
<telephone_number>
<number1>0000000000</number1>
<number2>1111111111</number2>
<number3>2222222222</number3>
<retries1>2</retries1>
<retries2>1</retries2>
<retries3>2</retries3>
<numberType1>Mobile</numberType1>
<numberType2>Fixnet</numberType2>
<numberType3>Fixnet</numberType3>
</telephone_number>
</contact>
<contact>
<id>2</id>
<firstName>firstname2</firstName>
<lastName>lastname2</lastName>
<department>SUPPORT</department>
<emailAddress>lastname2#mydomain.com</emailAddress>
<lineManagerId>22331470</lineManagerId>
<telephone_number>
<number1>3333333333</number1>
<number2>4444444444</number2>
<number3>5555555555</number3>
<retries1>2</retries1>
<retries2>1</retries2>
<retries3>2</retries3>
<numberType1>Mobile</numberType1>
<numberType2>Fixnet</numberType2>
<numberType3>Fixnet</numberType3>
</telephone_number>
</contact>
</schema_name>
</e_schema>
now with this piece of code I read each of the contact node and add them to a list
var xmlcontacts = xmlloaded.Descendants("schema_name").Where(node => (string)node.Attribute("value") == comboSchema.SelectedValue.ToString());
foreach (XElement subelement in xmlcontacts.Descendants("contact")) //element is variable
{
contact.Add(new Contact()
{
id = subelement.Element("id").Value,
firstName = subelement.Element("firstName").Value,
lastName = subelement.Element("lastName").Value,
department = subelement.Element("department").Value,
emailAddress = subelement.Element("emailAddress").Value,
lineManagerId = subelement.Element("lineManagerId").Value,
//_phonenumbers = phones
});
}
but I do not have any Idea how I can read the node with the telephone_number
can someone give a hint or a line of code how I can do that!
I assume that _phonenumbers is some kind of collection, e.g. an IEnumerable<PhoneInfo>:
_phoneNumbers = subelement.Element("telephone_number").Elements()
.Where(e => e.Name.LocalName.StartsWith("number").Select(e =>
new PhoneInfo
{
Number = e.Value,
Retries = subelement.Element("telephone_Number").Element(
"retries" + e.Name.LocalName.SubString(5)).Value,
NumberType = subelement.Element("telephone_Number").Element(
"numbertype" + e.Name.LocalName.SubString(5)).Value
})
The code uses a linq expression to create a PhoneInfo instance for each number, and it looks up the corresponding retries and number type.
As a note: The xml structure is quite bad, it would be much better to have all the numbers in <number> tags with the actual number being the content and type retries and type data being attributes of that node.
_phonenumbers = subelement.Descendants("telephone_number")
.Select(x =>
new List<string>() {
(string)x.Element("number1"),
(string)x.Element("number2"),
(string)x.Element("number3")
});
I think you need to loop thru "telephone_number" element inside "contact" element.
Below is the code you can try:
var xmlcontacts = xmlloaded.Descendants("schema_name").Where(node => (string)node.Attribute("value") == comboSchema.SelectedValue.ToString());
foreach (XElement subelement in xmlcontacts.Descendants("contact")) //element is variable
{
contact.Add(new Contact()
{
id = subelement.Element("id").Value,
firstName = subelement.Element("firstName").Value,
lastName = subelement.Element("lastName").Value,
department = subelement.Element("department").Value,
emailAddress = subelement.Element("emailAddress").Value,
lineManagerId = subelement.Element("lineManagerId").Value,
//_phonenumbers = phones
});
foreach (XElement phoneElement in subelement.Descendants("telephone_number"))
{
//add telephone_number details in list here
}
}
I have just added one more foreach inside the "contact" loop