I have this xml string:
<a:feed xmlns:a="http://www.w3.org/2005/Atom"
xmlns:os="http://a9.com/-/spec/opensearch/1.1/"
xmlns="http://schemas.zune.net/catalog/apps/2008/02">
<a:link rel="self" type="application/atom+xml" href="/docs" />
<a:updated>2014-02-12</a:updated>
<a:title type="text">Chickens</a:title>
<a:content type="html">eat 'em all</a:content>
<sortTitle>Chickens</sortTitle>
... other stuffs
<offers>
<offer>
<offerId>8977a259e5a3</offerId>
... other stuffs
<price>0</price>
... other stuffs
</offer>
</offers>
... other stuffs
</a:feed>
and want to get value of <price> but here in my codes:
XDocument doc = XDocument.Parse(xmlString);
var a = doc.Element("a");
var offers = a.Element("offers");
foreach (var offer in offers.Descendants())
{
var price = offer.Element("price");
var value = price.Value;
}
doc.Element("a"); returns null. I tried removing that line offers is also null. what is wrong in my code and how to get value of price? thanks
Here is correct way to get prices:
var xdoc = XDocument.Parse(xmlString);
XNamespace ns = xdoc.Root.GetDefaultNamespace();
var pricres = from o in xdoc.Root.Elements(ns + "offers").Elements(ns + "offer")
select (int)o.Element(ns + "price");
Keep in mind that your document have default namespace, and a is also namespace.
Get the namespace somehow, like
XNameSpace a = doc.Root.GetDefaultNamespace();
or, probably better:
XNameSpace a = doc.Root.GetNamespaceOfPrefix("a");
and then use it in your queries:
// to get <a:feed>
XElement f = doc.Element(a + "feed");
You can also set the namespace from a literal string, but then avoid var.
var xDoc = XDocument.Load(filename);
XNamespace ns = "http://schemas.zune.net/catalog/apps/2008/02";
var prices = xDoc
.Descendants(ns + "offer")
.Select(o => (decimal)o.Element(ns + "price"))
.ToList();
a is a namespace. To get the feed element try this:
XDocument doc = XDocument.Parse(xmlString);
XNamespace a = "http://www.w3.org/2005/Atom";
var feed = doc.Element(a + "feed");
Related
I have a xml file:
<?xml version="1.0" encoding="utf-8"?>
<ApplicationConfiguration xmlns="http://test.org/SDK/Configuration.xsd">
<ApplicationName>
<ApplicationUri>123</ApplicationUri>
<ApplicationUri>456</ApplicationUri>
</ApplicationName>
</ApplicationConfiguration>
What I want is set the value of ApplicationUri from 456 to 789 in C# code.
I wrote this code:
string docaddress = "testfile.xml";
XDocument doc = XDocument.Load(docaddress);
doc.Element("ApplicationConfiguration")
.Elements("ApplicationName").FirstOrDefault()
.SetElementValue("ApplicationUri", "789");
doc.Save(docaddress);
The problems are:
There is no error while running. I think the element ApplicationConfiguration is not correct. But when I delete the line xmlns=... from the xml file, it runs normally
The value 789 is replaced with 123, but not 456 as I want (same element name)
Can you tell me how to fix those problems?
Hello and welcome to Stack Overflow!
The xmlns attribute on the ApplicationConfiguration element makes that the root.
So you get the root first. Then you replace the values of each descendant, selecting locally by name with the element name you want. something like this:
string docaddress = "C:\\temp\\testfile.xml";
XDocument doc = XDocument.Load(docaddress);
var root = doc.Root;
var descendants = root.Descendants();
var these = root.Descendants().Where(p => p.Name.LocalName == "ApplicationUri");
foreach (var elem in these)
{
elem.Value = "789";
}
doc.Save(docaddress);
I added namespace to your code :
string docaddress = "testfile.xml";
XDocument doc = XDocument.Load(docaddress);
XNamespace ns = doc.Root.GetDefaultNamespace();
doc.Element(ns + "ApplicationConfiguration")
.Elements(ns + "ApplicationName").FirstOrDefault()
.SetElementValue(ns + "ApplicationUri", "789");
doc.Save(docaddress);
IMHO, here is an easiest method.
It is taking care of the XML default namespace.
No loops. Set based approach.
c#
void Main()
{
const string fileName = #"e:\temp\hala.xml";
const string searchFor = "456";
const string replaceWith = "789";
XDocument doc = XDocument.Load(fileName);
XNamespace ns = doc.Root.GetDefaultNamespace();
// step #1: find element based on the search value
XElement xmlFragment = doc.Descendants(ns + "ApplicationUri")
.Where(d => d.Value.Equals(searchFor)).FirstOrDefault();
// step #2: if found, set its value
if(xmlFragment != null)
xmlFragment.SetValue(replaceWith);
doc.Save(fileName);
}
I have an xml string like this
<Test>
<ConnectionParameters>
<ConnectionParameter DisplayName="asd" Id="cgfh" IsPassword="false" IsRequired="true"> </ConnectionParameter>
<ConnectionParameter DisplayName="asdasd" Id="fgh" IsPassword="false" IsRequired="true"></ConnectionParameter>
<ConnectionParameter DisplayName="asdasd" Id="hdfh" IsPassword="false" IsRequired="true"></ConnectionParameter>
<ConnectionParameter DisplayName="asdad" Id="dfgdf" IsPassword="false" IsRequired="true"> </ConnectionParameter>
</ConnectionParameters>
</Test>
How can I loop through each "ConnectionParameter" tag inorder to get the attributes like Id,DisplayName etc using xdocument?
I tried like this,
XDocument xdoc = new XDocument();
xdoc= XDocument.Parse(fileContent);
var saveResult = from b in xdoc.Descendants("ConnectionParameters")
select new
{
success = (string)b.Element("ConnectionParameter").Attribute("Id").Value ?? string.Empty,
};
But it only returns the first node only
You're currently looping through all the ConnectionParameters elements (of which there's only one) and selecting the first ConnectionParameter element (using the Element call). You want to just loop through the ConnectionParameter elements:
// Note the lack of creating a new XDocument for no reason
XDocument xdoc = XDocument.Parse(fileContent);
var saveResult = from b in xdoc.Descendants("ConnectionParameter")
select new
{
success = (string) b.Attribute("Id") ?? ""
};
Or to avoid creating an anonymous type for no obvious reason, and using plain "dot notation" as the query expression isn't helping you much:
XDocument xdoc = XDocument.Parse(fileContent);
var saveResult = xdoc.Descendants("ConnectionParameter")
.Select(b => (string) b.Attribute("Id") ?? "");
If you prefer to make the parent element names explicit, you could use:
XDocument xdoc = XDocument.Parse(fileContent);
var saveResult = xdoc.Element("Test")
.Element("ConnectionParameters")
.Descendants("ConnectionParameter")
.Select(b => (string) b.Attribute("Id") ?? "");
Or:
XDocument xdoc = XDocument.Parse(fileContent);
var saveResult = xdoc.Root
.Element("ConnectionParameters")
.Descendants("ConnectionParameter")
.Select(b => (string) b.Attribute("Id") ?? "");
Load your xml into xDocument, then you can do something like this (can't remember exact syntax)
xDoc.Root.Descendants("ConnectionParameters").Attribute("DisplayName").Value;
I have this xml string:
<a:feed xmlns:a="http://www.w3.org/2005/Atom"
xmlns:os="http://a9.com/-/spec/opensearch/1.1/"
xmlns="http://schemas.zune.net/catalog/apps/2008/02">
<a:link rel="self" type="application/atom+xml" href="/docs" />
<a:updated>2014-02-12</a:updated>
<a:title type="text">Chickens</a:title>
<a:content type="html">eat 'em all</a:content>
<sortTitle>Chickens</sortTitle>
... other stuffs
<offers>
<offer>
<offerId>8977a259e5a3</offerId>
... other stuffs
<price>0</price>
... other stuffs
</offer>
</offers>
... other stuffs
</a:feed>
and want to get value of <price> but here in my codes:
XDocument doc = XDocument.Parse(xmlString);
var a = doc.Element("a");
var offers = a.Element("offers");
foreach (var offer in offers.Descendants())
{
var price = offer.Element("price");
var value = price.Value;
}
doc.Element("a"); returns null. I tried removing that line offers is also null. what is wrong in my code and how to get value of price? thanks
Here is correct way to get prices:
var xdoc = XDocument.Parse(xmlString);
XNamespace ns = xdoc.Root.GetDefaultNamespace();
var pricres = from o in xdoc.Root.Elements(ns + "offers").Elements(ns + "offer")
select (int)o.Element(ns + "price");
Keep in mind that your document have default namespace, and a is also namespace.
Get the namespace somehow, like
XNameSpace a = doc.Root.GetDefaultNamespace();
or, probably better:
XNameSpace a = doc.Root.GetNamespaceOfPrefix("a");
and then use it in your queries:
// to get <a:feed>
XElement f = doc.Element(a + "feed");
You can also set the namespace from a literal string, but then avoid var.
var xDoc = XDocument.Load(filename);
XNamespace ns = "http://schemas.zune.net/catalog/apps/2008/02";
var prices = xDoc
.Descendants(ns + "offer")
.Select(o => (decimal)o.Element(ns + "price"))
.ToList();
a is a namespace. To get the feed element try this:
XDocument doc = XDocument.Parse(xmlString);
XNamespace a = "http://www.w3.org/2005/Atom";
var feed = doc.Element(a + "feed");
Bonjour,
I have an xml doc:
<ns2:feeds xmlns:ns2="XXXX" xmlns="XXXXX" version="3.0">
<ns2:feed>
<name>XXX</name>
</ns2:feed>
<ns2:feed>
<name>XXX</name>
</ns2:feed>
<ns2:feed>
<name>XXX</name>
</ns2:feed>
</ns2:feeds>
How can I use LinqToXml to get a list of Name properties? nothing I try seems to work...
var doc = XDocument.Load(#"feed.xml");
var names = doc
.XPathSelectElements("/*/*[localname()='feeds']") //What should the Xpath be, here?
.Select(p => new
{
Name = p.Descendants("name").First().Value
})
.ToList();
Is there a simple way to achieve this?
You can do this
XNamespace ns = XNamespace.Get("XXXX");
var listOfNames = doc.Descendants(ns + "feed")
.Select(x => x.Elements().First().Value).ToList();
+1 for lazyberezovsky's answer. If you need to specify the element name (name in this case) or you could have multiple name elements then you need to add a second namespace for those elements.
XNamespace ns2 = XNamespace.Get("XXXXX");
var listOfNames = doc.Root.Descendants(ns2 + "name").Select(x => x.Value).ToList();
With XPathSelectElements you should provide namespace manager in order to use namespaces in XPath query:
var manager = new XmlNamespaceManager(new NameTable());
manager.AddNamespace("ns2", "XXXX");
manager.AddNamespace("ns", "XXXXX"); // default namespace
var names = from n in xdoc.XPathSelectElements("//ns2:feed/ns:name", manager)
select (string)n;
Without XPath you should use XNamespace when providing name of node to find:
XNamespace ns = "XXXXX";
XNamespace ns2 = "XXXX";
var names = from f in xdoc.Descendants(ns2 + "feed")
select (string)f.Element(ns + "name");
To get rid of namespaces in XLinQ queries, use similar method mentioned below:
String xml_string = #"<ns2:feeds xmlns:ns2=""XXXX"" xmlns=""XXXXX"" version=""3.0"">
<ns2:feed>
<name>XXX</name>
</ns2:feed>
<ns2:feed>
<name>YYY</name>
</ns2:feed>
<ns2:feed>
<name>ZZZ</name>
</ns2:feed>
</ns2:feeds>";
var query = XElement.Parse(xml_string).Descendants()
.Where(c => c.Name.LocalName.ToString() == "name")
.ToArray();
foreach (String item in query)
{
Console.WriteLine(item);
}
I am trying to process the following XML:
<rif:Rif xmlns:rif="rif" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" rif:numeroRif="XYZXYZXYZ">
<rif:Nombre>Nombre</rif:Nombre>
<rif:AgenteRetencionIVA>SI</rif:AgenteRetencionIVA>
<rif:ContribuyenteIVA>SI</rif:ContribuyenteIVA>
<rif:Tasa />
</rif:Rif>
An I am using the next code:
XDocument doc = XDocument.Parse(result);
var q = from item in doc.Descendants()
let attributeType = item.Attribute("AgenteRetencionIVA").Value
select item;
I have problems to get the attribute rif:AgenteRetencionIVA. How do I to do it?
Looks like tou need to specify custom namespace:
string xml = #"...";
XName nameRif = "rif";
XDocument doc = XDocument.Parse(xml);
var q = from item in doc.Descendants()
let attributeType = item.Attribute(nameRif + "AgenteRetencionIVA")
select item.Value;
var a = q.ToArray();