xelement

How to get specific element Count in XML or XElement variable

拜拜、爱过 提交于 2019-11-26 21:46:33
问题 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#? 回答1: You can filter the

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

若如初见. 提交于 2019-11-26 16:49:15
问题 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? 回答1: 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

parsing XML with ampersand

不问归期 提交于 2019-11-26 12:48:22
问题 I have a string which contains XML, I just want to parse it into Xelement, but it has an ampersand. I still have a problem parseing it with HtmlDecode. Any suggestions? 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(\">\", \">\")

Best way to get InnerXml of an XElement?

不问归期 提交于 2019-11-26 10:17:45
What's the best way to get the contents of the mixed body element in the code below? The element might contain either XHTML or text, but I just want its contents in string form. The XmlElement type has the InnerXml property which is exactly what I'm after. The code as written almost does what I want, but includes the surrounding <body> ... </body> element, which I don't want. XDocument doc = XDocument.Load(new StreamReader(s)); var templates = from t in doc.Descendants("template") where t.Attribute("name").Value == templateName select new { Subject = t.Element("subject").Value, Body = t

How to read large xml file without loading it in memory and using XElement

人走茶凉 提交于 2019-11-26 07:47:23
问题 I want to read a large xml file (100+M). Due to its size, I do not want to load it in memory using XElement. I am using linq-xml queries to parse and read it. What\'s the best way to do it? Any example on combination of XPath or XmlReader with linq-xml/XElement? Please help. Thanks. 回答1: Yes, you can combine XmlReader with the method XNode.ReadFrom, see the example in the documentation which uses C# to selectively process nodes found by the XmlReader as an XElement. 回答2: The example code in