Reputation: 835
I need to get the value of Description0, ItemCode
from this XML.
<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="http://afasfs/services/Exact.Entity.REST.EG/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<id>http://afasfs/services/Exact.Entity.REST.EG/Item/</id>
<title type="text">Item</title>
<link rel="self" title="Item" href="Item" />
<entry>
<id>http://afasfs/services/Exact.Entity.REST.EG/Item('00X0')</id>
<link rel="edit" title="Item" href="Item('00X0')" />
<title />
<content type="application/xml">
<m:properties>
<d:Description0>HEX S/DRILL SCREW NEO 14-10 X 75 C4 (PK100)</d:Description0>
<d:ItemCode>0V0X0sdfA</d:ItemCode>
</m:properties>
</content>
</entry>
</feed>
I have tried to do the following but none of them works and having this error
The ':' character, hexadecimal value 0x3A, cannot be included in a name.
var itemElements = xDoc.Descendants("feed");
var itemElements = xDoc.Element("feed");
var itemElements = xDoc.Elements("feed");
var itemElements = xDoc.Root.Elements("feed");
var itemElements = xDoc.Elements("feed").Element("entry");
foreach (XElement elem in itemElements.Elements()){}
Thank you in advance for the help.
Upvotes: 0
Views: 123
Reputation: 4903
You could use Namespaces to get values :
1 - Declare all Namespace
XNamespace xn = "http://www.w3.org/2005/Atom";
XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
2 - Query to get Description0, ItemCode
:
var properties = xDoc
.Descendants(xn + "entry")
.Descendants(m + "properties")
.Select(p => new { Description = p.Element(d + "Description0")?.Value, ItemCode = p.Element(d + "ItemCode")?.Value })
.FirstOrDefault();
3 - All code :
string xml = @"
<feed xml:base=""http://afasfs/services/Exact.Entity.REST.EG/"" xmlns=""http://www.w3.org/2005/Atom"" xmlns:d=""http://schemas.microsoft.com/ado/2007/08/dataservices"" xmlns:m=""http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"">
<id>http://afasfs/services/Exact.Entity.REST.EG/Item/</id>
<title type=""text"">Item</title>
<link rel=""self"" title=""Item"" href=""Item"" />
<entry>
<id>http://afasfs/services/Exact.Entity.REST.EG/Item('00X0')</id>
<link rel=""edit"" title=""Item"" href=""Item('00X0')"" />
<title />
<content type=""application/xml"">
<m:properties>
<d:Description0>HEX S/DRILL SCREW NEO 14-10 X 75 C4 (PK100)</d:Description0>
<d:ItemCode>0V0X0sdfA</d:ItemCode>
</m:properties>
</content>
</entry>
</feed>";
XDocument xDoc = XDocument.Parse(xml);
XNamespace xn = "http://www.w3.org/2005/Atom";
XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
var properties = xDoc
.Descendants(xn + "entry")
.Descendants(m + "properties")
.Select(p => new { Description = p.Element(d + "Description0")?.Value, ItemCode = p.Element(d + "ItemCode")?.Value })
.FirstOrDefault();
Console.WriteLine($"Description0:{ properties.Description} , ItemCode:{properties.ItemCode}");
Result
Description0:HEX S/DRILL SCREW NEO 14-10 X 75 C4 (PK100) , ItemCode:0V0X0sdfA
I hope you find this helpful.
Upvotes: 2