linq-to-xml

How to make an if statement to show xml attribute based on current time

对着背影说爱祢 提交于 2019-12-10 16:27:10
问题 I have a XML like this: <PrayerTime Day ="1" Month="5" Fajr="07:00" Sunrise="09:00" Zuhr="14:00" /> A class like this: public class PrayerTime { public string Fajr { get; set; } public string Sunrise { get; set; } public string Zuhr { get; set; } } And something to get the value like this: XDocument loadedCustomData = XDocument.Load("WimPrayerTime.xml"); var filteredData = from c in loadedCustomData.Descendants("PrayerTime") where c.Attribute("Day").Value == myDay.Day.ToString() && c

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary

▼魔方 西西 提交于 2019-12-10 15:08:52
问题 I receive the above error message when performing a unit test on a method. I know where the problem is at, I just don't know why it's not present in the dictionary. Here is the dictionary: var nmDict = xelem.Descendants(plantNS + "Month").ToDictionary( k => new Tuple<int, int, string>(int.Parse(k.Ancestors(plantNS + "Year").First().Attribute("Year").Value), Int32.Parse(k.Attribute("Month1").Value), k.Ancestors(plantNS + "Report").First().Attribute("Location").Value.ToString()), v => { var

XDocument.Root.Element returns null

孤者浪人 提交于 2019-12-10 14:53:05
问题 I have XML which is like: <?xml version="1.0" encoding="utf-16"?> <RootNodeName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" MyAttribute="7" xmlns="mylink"> <IsValid>false</IsValid> <Name>some matrix</Name> ...Some more nodes... </RootNodeName> and code wich is like: var doc = XDocument.Parse(myXmlString); Console.WriteLine(doc.Root.Element("Name")); and console shows just an empty space since doc.Root.Element("Name") returns null =( While

How to get an element that has : in its name?

不问归期 提交于 2019-12-10 14:46:56
问题 I need to get the CountryName from this XML: http://api.hostip.info/?ip=12.215.42.19 The response XML is: <HostipLookupResultSet version="1.0.1" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.hostip.info/api/hostip-1.0.1.xsd"> <gml:description>This is the Hostip Lookup Service</gml:description> <gml:name>hostip</gml:name> <gml:boundedBy> <gml:Null>inapplicable</gml:Null> </gml:boundedBy> <gml:featureMember

LINQ to XML Selecting Child Elements

此生再无相见时 提交于 2019-12-10 14:37:15
问题 I am trying to extract information from an XML file into an object using LINQ to XML. Although I can return the document and section Id attributes I cannot get access to the Items for each section element, it returns an IEnumerable of all the items in the document. I know this is correct as I’m calling Descendants but am struggling to get it to return only the child items of each section element. Can anybody help? XML Document <root> <document id="1"> <section id="1.1"> <item id="1.1.1"><

Mono - XDocument.Load fails with LoadOptions.PreserveWhitespace

爱⌒轻易说出口 提交于 2019-12-10 14:19:19
问题 Using Mono version 2.10.5, the following code fails on any XML document: using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Xml.Linq; namespace TestXDocument { class MainClass { public static void Main (string[] args) { Stream s = File.Open("Settings.xml", FileMode.Open); XDocument d = XDocument.Load(s, LoadOptions.PreserveWhitespace); s.Close(); d.Save("Settings.xml"); } } } This only happens if XDocument.Load uses LoadOptions.PreserveWhitespace.

Add namespaces with and without names to an XElement

妖精的绣舞 提交于 2019-12-10 14:03:06
问题 I need so generate XML like the following: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <url> <loc>http://www.xyz.eu/</loc> <lastmod>2010-01-20T10:56:47Z</lastmod> <changefreq>daily</changefreq> <priority>1</priority> </url> <url> <loc>http://www.xyz.eu/2/</loc> <lastmod>2009-10-13T10:20:03Z</lastmod> <changefreq>daily</changefreq> <priority>0.5</priority> </url> <url>

LINQ to XML - Get given XElement's text content without child elements' text content

依然范特西╮ 提交于 2019-12-10 13:38:23
问题 I have just started using LINQ to XML, and I am having trouble getting the text contents of a given XElement without getting the text contents of all the child elements as well. So for example, if I have the following XML document: <?xml version="1.0" encoding="utf-8" ?> <root xmlns="example.org/rootns">This is root value <children> <child name='child 1'>value 1</child> <child name='child 2'>value 2 <grandchild name='grandchild A'>value A</grandchild> </child> </children> </root> And I use

Adding a namespace to existing XDocument

泄露秘密 提交于 2019-12-10 13:26:23
问题 I need to manipulate some xml files using Linq to xml. I have an existing XDocument that I Load Now I cannot seem to be able to add a namespace to it. I do: //Load an existing xml into a XDocument XDocument xdoc=XDocument.Load(myXml); //Create a namespace XNamespace myNS="http://www.w3.org/2001/XMLSchema-instance/MyShinyNewNamespace"; xAttribute myAttr=new XAttribute(XNamespace.Xmlns +"myNS",myNS); //Add new namepsace to root xdoc.Root ???? What do you do here? How do I retrieve my namespace?

XPathDocument vs. XmlDocument

前提是你 提交于 2019-12-10 13:03:13
问题 I'm developing a blogging engine in ASP.NET and one the repositories is implemented to use XML files as the data store. the XML repository will mainly be used for local use and testing purposes. now although it might not be that of an issue with today's computers which have lots of memory available and processing power, but nevertheless I wanted to know some specific details : XPathDocument is a read-only document whereas the XmlDocument is a read/write document. Hence, is XPathDocument more