I want to get data from xml but there are lots of tags, fields and value keys. I couldn't select the value which I want. How can I select the "Error" value from this XML with C#?
<?xml version="1.0" encoding="UTF-8"?>
<Database xmlns="http://www.example.com/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Datas>
<Data name="sMsg" access="private" xsi:type="collection" type="string">
<Value key="Cycle" value="deger1" />
<Value key="Error" value="deger2" />
<Value key="Info" value="deger3" />
<Value key="Jog" />
<Value key="Warning" />
</Data>
<Data name="tTabla" access="private" xsi:type="array" type="tabla" size="1">
<Field name="dddd" xsi:type="array" type="bool" size="1" />
<Field name="ssss" xsi:type="array" type="bool" size="1" />
<Field name="aaaa" xsi:type="array" type="num" size="1" />
<Field name="rrrr" xsi:type="collection" type="num">
<Value key="Actuel" />
<Value key="Expected" />
</Field>
</Data>
</Datas>
</Database>
You can try this way :
var doc = XDocument.Parse(xml);
XNamespace d = doc.Root.GetDefaultNamespace();
var result = (string)
doc.Descendants(d + "Data")
.Elements(d + "Value")
.FirstOrDefault(o => (string) o.Attribute("key") == "Error")
.Attribute("value");
Console.WriteLine(result);
Try this it returns the XElement with Key equal to Error
XDocument m = XDocument.Load(#"Path");
var res = m.Descendants().Where(x => x.Name.LocalName.Equals("Value") && x.Attribute("key") != null && x.Attribute("key").Value.Equals("Error")).FirstOrDefault();
If there are multiple "Error" values for your attributes, you can do:
IEnumerable<XAttribute> answer = xml.Descendants().Attributes().Where(node => node.Value == "Error");
foreach (var xAttribute in answer)
{
Console.WriteLine(xAttribute.Value);
}
If you only want the first or there is only one:
string answer = xml.Descendants().Attributes().FirstOrDefault(node => node.Value == "Error");
Note FirstOrDefault may yield null if it doesn't find any "Error" values inside your xml.
These queries are done using LINQ To XML, i strongly encourage you to read up.
Related
I have this result from an httpGet and i want to extract some values from it.
<?xml version="1.0" encoding="UTF-8"?>
<ns4:endpoint xmlns:ns4="identity.ers.ise.cisco.com" xmlns:ers="ers.ise.cisco.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema" description="Beskriv" id="eed9d9a0-5c8d-11ea-8778-
9e4294fe2fb6" name="1A:1A:1A:1A:1A:1A">
<link rel="self" href="https://10.100.10.10:9060/ers/config/endpoint/eed9d9a0-5c8d-11ea-8778-
9e4294fe2fb6" type="application/xml" />
<customAttributes>
<customAttributes>
<entry>
<key>Changed By API</key>
<value />
</entry>
<entry>
<key>TAG name</key>
<value>TagNavn</value>
</entry>
<entry>
<key>API Change Date</key>
<value>2020-03-02</value>
</entry>
<entry>
<key>API Changed By</key>
<value>Mot</value>
</entry>
</customAttributes>
</customAttributes>
<groupId>aa13bb40-8bff-11e6-996c-525400b48521</groupId>
<identityStore />
<identityStoreId />
<mac>1A:1A:1A:1A:1A:1A</mac>
<portalUser />
<profileId />
<staticGroupAssignment>true</staticGroupAssignment>
<staticProfileAssignment>false</staticProfileAssignment>
</ns4:endpoint>
I can extract the "name" and "description" by using xmlnode.value, but im not sure how to access my custom attributes such as <mac> and <groupid>
Are they regarded as child nodes?
Any help is appreciated.
Something as simple as (using XDocument class from System.Xml.Linq namespace):
// You don't need below line
// XDocument xml = XDocument.Load(#"path to txt file");
var macElement = xml.Descendants().Where(element => element.Name == "mac").FirstOrDefault();
// Similairly for groupid elemnt
var groupidElement = xml.Descendants().Where(element => element.Name == "groupid").FirstOrDefault();
if(macElement != null)
{
// do something with macElement.Value
}
if(groupidElement != null)
{
// do something with groupidElement.Value
}
Also call to xml.Descendants() is reduntant, so you could store it in variable like:
var descendants = xml.Descendants();
and reuse it.
I am attempting to retrieve values from certain nodes in an XML response by using XElement where the attribute value is "id=x".
This is what the XML response looks like.
<customfields>
<group id="6" title="Help Desk Ticket Categories" displayorder="1">
<field id="73" title="Second level classification" type="6" name="zscyz3a30h1q"><![CDATA[-- unassigned --]]></field>
</group>
<group id="9" title="For CS" displayorder="4">
<field id="82" title="CS Root Cause Classification " type="6" name="pye19ntzyp8v"><![CDATA[--Unassigned--]]></field>
<field id="84" title="Staff Responsible" type="6" name="7gy8bfu8tidv"><![CDATA[-- unassigned --]]></field>
<field id="93" title="Customer Package" type="6" name="fxk9rqtlw0fa"><![CDATA[--Unassigned--]]></field>
</group>
<group id="2" title="Client Info" displayorder="3">
<field id="5" title="Customer Name" type="1" name="77004d0323"><![CDATA[BigG]]></field>
<field id="17" title="Account Number" type="1" name="d1abccc1a0" /><![CDATA[123]]></field>
<field id="16" title="Contact Name" type="1" name="93d2b9adf1" /><![CDATA[george]]></field>
<field id="13" title="Phone No." type="1" name="0aa31be4b8" /><![CDATA[456]]></field>
</group>
</customfields>
This is the code I'm using now (which works), but I'd rather retrieve values for field with "id"="5" rather than have the value retrieved through a descendant index.
XElement root = XElement.Parse(GetTicketCustomFields(ticketID));
kCustomer = (string)(from el in root.Descendants("field") select el).ElementAt(4);
kAccountNumber = (string)(from el in root.Descendants("field") select el).ElementAt(5);
kName = (string)(from el in root.Descendants("field") select el).ElementAt(6);
kPhone = (string)(from el in root.Descendants("field") select el).ElementAt(7);
Thanks for
I would use XPath
var root = XDocument.Parse(xmlstring);
var accountNumber = root.XPathSelectElement("//field[#id='17']").Value;
or
var accountNumber = root.XPathSelectElement("//field[#title='Account Number']").Value;
Using Linq2Xml is also possible
var accountNumber = root.Descendants("field")
.FirstOrDefault(F => F.Attribute("id").Value == "17")
.Value;
Use XPath as #L.B. suggests.
If you're using Visual Studio there's a plugin written by yours truly which can help you find the XPath of a given element or attribute, to get you on your way: XPath Information
There are some fields on my xml document which are same value but under different name. I want to select the value of "Error" on this xml document and so want to display "deger2". Also I want to display "deger5" How can I do this?
<?xml version="1.0" encoding="UTF-8"?>
<Database xmlns="http://www.example.com/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Datas>
<Data name="sMsg" access="private" xsi:type="collection" type="string">
<Value key="Cycle" value="deger1" />
<Value key="Error" value="deger2" />
<Value key="Info" value="deger3" />
<Value key="Jog" />
<Value key="Warning" />
</Data>
<Data name="tTabla" access="private" xsi:type="array" type="tabla" size="1">
<Value key="Cycle" value="deger4" />
<Value key="Error" value="deger5" />
<Value key="Info" value="deger6" />
<Value key="Jog" />
<Value key="Warning" />
</Data>
</Datas>
</Database>
You should take the xml namespace into considiration. Using Linq to Xml
var xDoc = XDocument.Parse(xmlstring); //XDocument.Load(filename)
XNamespace ns = "http://www.example.com/2";
var errors = xDoc.Descendants(ns + "Value") //<-- See the usage of ns
.Where(d => (string)d.Attribute("key") == "Error")
.Select(d => (string)d.Attribute("value"))
.ToList();
EDIT
Is there any way to select like this: select error value where Data name="tTabla"?
var errors = xDoc.Descendants(ns + "Data")
.First(d => (string)d.Attribute("name") == "tTabla")
.Descendants(ns + "Value")
.First(d => (string)d.Attribute("key") == "Error")
.Attribute("value")
.Value;
EDIT2
You can also use XPATH
var nsmgr = new XmlNamespaceManager(xDoc.CreateNavigator().NameTable);
nsmgr.AddNamespace("ns", "http://www.example.com/2");
var errors = xDoc.XPathSelectElement("//ns:Data[#name='tTabla']/ns:Value[#key='Error']", nsmgr)
.Attribute("value")
.Value;
U can also use XPath to select them:
XmlNodeList nodeList = root.SelectNodes("//Data/Value[#key="Error"]");
In this xml file (http://www.studiovincent.net/list.xml):
<list version="1.0">
<meta>
<type>resource-list</type>
</meta>
<resources start="0" count="4">
<resource classname="Quote">
<field name="name">Vincent</field>
<field name="username">Hill</field>
<field name="age">31</field>
<field name="hair">black</field>
</resource>
<resource classname="Quote">
<field name="name">John</field>
<field name="username">Tedelon</field>
<field name="age">27</field>
<field name="hair">brown</field>
</resource>
<resource classname="Quote">
<field name="name">Michael</field>
<field name="username">Lopez</field>
<field name="age">20</field>
<field name="hair">red</field>
</resource>
<resource classname="Quote">
<field name="name">Frank</field>
<field name="username">Lopez</field>
<field name="age">25</field>
<field name="hair">black</field>
</resource>
</resources>
</list>
using this code:
using System.Xml;
using.System.Xml.Linq;
XmlReader reader = XmlReader.Create("http://www.studiovincent.net/list.xml");
XElement el = XElement.Load(reader);
reader.Close();
var items = el.Elements("resources").Elements("resource").Descendants().DescendantNodes();
var items = from item in el.Elements("resources").Elements("resource").Descendants()
where item.Attribute("name").Value == "name" select item.FirstNode;
foreach (XNode node in items)
{
Console.WriteLine(node.ToString());
}
I have this OUTPUT:
Vincent
John
Michael
Frank
Code working very good, but I need get value 31 which corresponds field name="age" where field name="name" is Vincent. How Can I get this result?
I recommend you do as you would when reading XML naturally.
In your code, try to find all the fields with the name attribute set to "name".
This process cannot be used to associate a name with an age. It is more natural to read the XML and check all resource elements. Then add to this element some information described in the field elements.
// Using LINQ to SQL
XDocument document = XDocument.Load("http://www.studiovincent.net/list.xml"); // Loads the XML document.
XElement resourcesElement = document.Root.Element("resources"); // Gets the "resources" element that is in the root "list" of the document.
XElement resourceElementVincent = (from resourceElement in resourcesElement.Elements("resource")// Gets all the "resource" elements in the "resources" element
let fieldNameElement = resourceElement.Elements("field").Single(fieldElement => fieldElement.Attribute("name").Value == "name") // Gets the field that contains the name (there one and only one "name" field in the "resource" element -> use of Single())
where fieldNameElement.Value == "Vincent" // To get only resources called "Vincent"
select resourceElement).Single(); // We suppose there is one and only 1 resource called "Vincent" -> Use of Single()
XElement fieldAgeElement = resourceElementVincent.Elements("field").Single(fieldElement => fieldElement.Attribute("name").Value == "age"); // Gets the corresponding "age" field
int age = int.Parse(fieldAgeElement.Value, CultureInfo.InvariantCulture); // Gets the age by Parse it as an integer
Console.WriteLine(age);
Does it do what you want?
Consider this below XML is there as one of the SQL table's column.
<Root>
<Name>Dinesh</Name>
<Id>2</Id>
</Root>
The objective of the query is to fetch the Name from the XML. In this example we will fetch the 'Dinesh' as the value.
var Query = (from t in dbContext.Employee.AsEnumerable()
where t.active == true
select new Employee
{
Id = t.AtpEventId,
Name = XDocument.Parse(t.Content).Descendants("Root").Descendants("Name").ToList().
Select(node => node.Value.ToString()).FirstOrDefault()
});
Note the following:
t.active == true is just an example to make some condition if needed.
Please note, in the above LINQ query, always use the AsEnumerable, as I did in the first line.
Descendants("Root").Descendants("Name") - here "Root" should be the element matching with the XML, and under Root we have Name element.
I'm looking to get to value further in the XML code than I've seen examples for and with more attributes to consider.
The XML code is:
<?xml version="1.0"?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<database name="Members">
<table_structure name="Logins">
<field Field="ID" Type="int(100)" Null="NO" Key="PRI" Extra="auto_increment" Comment="" />
<field Field="Username" Type="text" Null="YES" Key="" Extra="" Comment="" />
<field Field="Password" Type="text" Null="YES" Key="" Extra="" Comment="" />
<key Table="Logins" Non_unique="0" Key_name="PRIMARY" Seq_in_index="1" Column_name="ID" Collation="A" Cardinality="1" Null="" Index_type="BTREE" Comment="" />
<options Name="Logins" Engine="MyISAM" Version="10" Row_format="Dynamic" Rows="1" Avg_row_length="20" Data_length="40" Max_data_length="281474976710655" Index_length="2048" Data_free="20" Auto_increment="3" Create_time="2011-10-14 19:30:57" Update_time="2011-10-14 19:32:21" Collation="latin1_swedish_ci" Create_options="" Comment="" />
</table_structure>
<table_data name="Logins">
<row>
<field name="ID">1</field>
<field name="Username">MyName</field>
<field name="Password">MyPassowrd</field>
</row>
<row>
<field name="ID">2</field>
<field name="Username">MyName2</field>
<field name="Password">MyPassowrd2</field>
</row>
</table_data>
</database>
</mysqldump>
I'm trying to get to 1, MyName and Password to then enter them into an SQL database.
The issue I'm having is getting through the multiple levels:
<database name="Members">
<table_data name="Logins">
<row>
<field name="ID">
With all the examples I'm finding only ones showing 1 or 2 levels.
This seems to work,
XElement xe = XElement.Load("C:\\PATH.xml");
int count = xe.Descendants("row").Count();
var items = xe.Descendants("field").Where(n => (string)n.Attribute("name") == "ID").ToList();
var items2 = xe.Descendants("field").Where(n => (string)n.Attribute("name") == "Username").ToList();
var items3 = xe.Descendants("field").Where(n => (string)n.Attribute("name") == "Password").ToList();
for (int i = 0; i < count; i++)
{
string res = items[i].Value.ToString();
string res2 = items2[i].Value.ToString();
string res3 = items3[i].Value.ToString();
}
I've got an ID, but I now need all its other elements in its row.
That is,
if (id = 2)
{
string name = username.value;
string password = password.value;
}
where name will result in myName2 and password will be MyPassword2.
I don't want to put usernames and passwords in a list.
Any advice again?
Try with LINQ to XML.
Here you have a tutorial.