xelement

Children of XElement

帅比萌擦擦* 提交于 2019-11-29 02:02:33
问题 How do I get just the children of an XElement? I am currently using the XElement.Descendants() function, which returns all levels of XElements, rather than just the child nodes. What I would really like is an IEnumerable of just the children. 回答1: The immediate child elements of one XElement are accessible by calling the Element() or Elements() functions. Use the overloads with a name to access specific elements, or without to access all child elements. There are also similar methods like

XElement => Add children nodes at run time

◇◆丶佛笑我妖孽 提交于 2019-11-28 20:55:33
问题 So let's assume this is what i want to achieve: <root> <name>AAAA</name> <last>BBBB</last> <children> <child> <name>XXX</name> <last>TTT</last> </child> <child> <name>OOO</name> <last>PPP</last> </child> </children> </root> Not sure if using XElement is the simplest way but this is what I have so far: XElement x = new XElement("root", new XElement("name", "AAA"), new XElement("last", "BBB")); Now I have to add the "children" based on some data i have. There could be 1,2,3,4 ... so I need to

Get Last Element in C# using XElement

拜拜、爱过 提交于 2019-11-28 14:19:30
I have a XML feed loaded in an XElement. The structure is <root> <post></post> <post></post> <post></post> <post></post> . . . . <post></post> </root> I want to directly get the value of the Last post. How I do that using XElement in C#. Thanks. R4j Or try this to get XElement: XDocument doc = XDocument.Load("yourfile.xml"); XElement root = doc.Root; Console.WriteLine(root.Elements("post").Last()); You can use LastNode property on root element: XElement root = doc.Root; XElement lastPost = (XElement)root.LastNode; var doc = XDocument.Parse(xml); var lastPost = doc.Descendants("post").Last();

C# - How to remove xmlns from XElement

≯℡__Kan透↙ 提交于 2019-11-28 07:47:37
问题 How can I remove the "xmlns" namespace from a XElement? I tried: attributes.remove, xElement.Name.NameSpace.Remove(0), etc, etc. No success. My xml: <event xmlns="http://www.blablabla.com/bla" version="1.00"> <retEvent version="1.00"> </retEvent> </event> How can I accomplish this? 回答1: The accepted answer did not work for me because xelement.Attributes() was empty, it wasn't returning the namespace as an attribute. The following will remove the declaration in your case: element.Name =

How to get specific element Count in XML or XElement variable

不羁的心 提交于 2019-11-28 00:43:24
Consider this XML: <Employees> <Person> <ID>1000</ID> <Name>Nima</Name> <LName>Agha</LName> </Person> <Person> <ID>1001</ID> <Name>Ligha</Name> <LName>Ligha</LName> </Person> <Person> <ID>1002</ID> <Name>Jigha</Name> <LName>Jigha</LName> </Person> <Person> <ID>1003</ID> <Name>Aba</Name> <LName>Aba</LName> </Person> </Employees> I declare a XElement variable and create the XML assigning it to that. How I can get count of ID elements in this XML variable in C#? You can filter the descendant elements using the Descendants method with the name "ID", then count the results: int count = xml

How to get XElement's value and not value of all child-nodes?

[亡魂溺海] 提交于 2019-11-27 14:36:08
Sample xml: <parent> <child>test1</child> <child>test2</child> </parent> If I look for parent.Value where parent is XElement, I get "test1test2". What I am expecting is "". (since there is no text/value for . What property of XElement should I be looking for? When looking for text data in the <parent> element you should look for child nodes that have NodeType properties equal to XmlNodeType.Text . These nodes will be of type XText . The following sample illustrates this: var p = XElement .Parse("<parent>Hello<child>test1</child>World<child>test2</child>!</parent>"); var textNodes = from c in p

Remove empty/blanks elements in collection of XML nodes

南楼画角 提交于 2019-11-27 14:25:49
I have an XML document like this: <magento_api> <data_item> <code>400</code> <message>Attribute weight is not applicable for product type Configurable Product</message> </data_item> <data_item> <code>400</code> <message>Resource data pre-validation error.</message> </data_item> <data_item> <code>1</code> <message></message> </data_item> <data_item> <code></code> <message>No code was given</message> </data_item> </magento_api> I'm trying to iterate each node and do the following: Throw out any elements that are empty/blank. Generate new Node with only elements containing values. Send the

Get Last Element in C# using XElement

淺唱寂寞╮ 提交于 2019-11-27 08:26:30
问题 I have a XML feed loaded in an XElement. The structure is <root> <post></post> <post></post> <post></post> <post></post> . . . . <post></post> </root> I want to directly get the value of the Last post. How I do that using XElement in C#. Thanks. 回答1: Or try this to get XElement: XDocument doc = XDocument.Load("yourfile.xml"); XElement root = doc.Root; Console.WriteLine(root.Elements("post").Last()); 回答2: You can use LastNode property on root element: XElement root = doc.Root; XElement

How to use XPath with XElement or LINQ?

断了今生、忘了曾经 提交于 2019-11-27 06:51:34
Consider the following XML: <response> <status_code>200</status_code> <status_txt>OK</status_txt> <data> <url>http://bit.ly/b47LVi</url> <hash>b47LVi</hash> <global_hash>9EJa3m</global_hash> <long_url>http://www.tumblr.com/docs/en/api#api_write</long_url> <new_hash>0</new_hash> </data> </response> I'm looking for a really short way to get just the value of the <hash> element. I tried: var hash = xml.Element("hash").Value; But that's not working. Is it possible to provide an XPath query to an XElement ? I can do it with the older System.Xml framework, doing something like: xml.Node("/response

parsing XML with ampersand

爱⌒轻易说出口 提交于 2019-11-27 05:24:55
I have a string which contains XML, I just want to parse into Xelement, but it has an ampersand. I still have problem to parse it with HtmlDecode. Any suggestion? string test = " <MyXML><SubXML><XmlEntry Element="test" value="wow&" /></SubXML></MyXML>"; XElement.Parse(HttpUtility.HtmlDecode(test)); I also added these methods to replace those characters, but I am still getting XMLException. string encodedXml = test.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "&apos;"); XElement myXML = XElement.Parse(encodedXml); t or Even tried it with this: string