xelement

How to add an existing Xml string into a XElement

巧了我就是萌 提交于 2019-12-17 20:34:34
问题 How to add an existing Xml string into a XElement? This code var doc = new XDocument( new XElement("results", "<result>...</result>") ); of course produces this <results><result></result></results> but I need this <results><result>...</result></results> Any ideas? 回答1: This should work: var xmlString = "<result>sometext</result>"; var xDoc = new XDocument(new XElement("results", XElement.Parse(xmlString))); 回答2: The answer by Sani Singh Huttunen put me on the right track, but it only allows

Remove empty/blanks elements in collection of XML nodes

我的梦境 提交于 2019-12-17 12:02:29
问题 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

How to use XPath with XElement or LINQ?

我的未来我决定 提交于 2019-12-17 08:34:27
问题 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

Get the XPath to an XElement?

扶醉桌前 提交于 2019-12-17 03:04:21
问题 I've got an XElement deep within a document. Given the XElement (and XDocument?), is there an extension method to get its full (i.e. absolute, e.g. /root/item/element/child ) XPath? E.g. myXElement.GetXPath()? EDIT: Okay, looks like I overlooked something very important. Whoops! The index of the element needs to be taken into account. See my last answer for the proposed corrected solution. 回答1: The extensions methods: public static class XExtensions { /// <summary> /// Get the absolute XPath

Best way to get InnerXml of an XElement?

爱⌒轻易说出口 提交于 2019-12-17 01:40:40
问题 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

Create a List from XElements Dynamically

隐身守侯 提交于 2019-12-16 18:03:13
问题 I am reading a bunch of XML files into a list (IEnumerable really) of XElements. Then I want to convert the XElement list (these XElements contain a bunch of child-elements) into a list of classes, so I can do later operations with the data more easily. Now if I know in advance the structure of XElements, this would be easy; I'd just create a class that mimics the XElement structure and fill instances of it with the XElement contents. But here's the caveat; my XML file element structure is

How to create an XML of this structure

我的未来我决定 提交于 2019-12-13 21:14:09
问题 I'd like to create an xml structure like below: <root> <element name= "text here 1"> <child>asd</child> <child>asd</child> </element> <element name= "text here 2"> <child>asd</child> <child>asd</child> </element> </root> I'm familiar with XElement doc = XElement.Load(mainDirectory); XElement newElem = new XElement("element", new XElement(child, ""), new XElement(child, "")); doc.Add(newElem); doc.Save(mainDirectory); So I think this falls down on how to add the "attribute" when I am creating

XElement default namespace on attributes provides unexpected behaviour

丶灬走出姿态 提交于 2019-12-13 15:09:58
问题 I am having trouble creating an XML document that contains a default namespace and a named namespace, hard to explain easier to just show what I am trying to produce... <Root xmlns="http://www.adventure-works.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.SomeLocatation.Com/MySchemaDoc.xsd"> <Book title="Enders Game" author="Orson Scott Card" /> <Book title="I Robot" author="Isaac Asimov" /> </Root> but what I end up with is this... <Root xmlns="http

How to insert IEnumerable<XElement> object into SQL Server table?

别来无恙 提交于 2019-12-13 04:25:05
问题 I have created below code to get Users from XML: string userName = "user"; string password = "password"; string uri = @"uri"; WebClient webClient = new WebClient(); webClient.Credentials = new NetworkCredential(userName, password); Stream stream = webClient.OpenRead(uri); XElement output = XElement.Load(stream); IEnumerable<XElement> users = from el in output.Elements() select el; foreach (XElement str in users) { Console.WriteLine(str); } Result: <User> <ObjectId>3cbde</ObjectId> <Alias

XElement/LinQ Code Failure

痴心易碎 提交于 2019-12-12 05:28:40
问题 So I have the following code: public static void Replace(filepath) { try { XElement xD = XElement.Load(filePath); foreach (XElement xE in xD.Elements()) { Console.WriteLine(xE.Attribute("attr").Value); if (tuv.Attribute("attr") != null) { Console.WriteLine(xE.Attribute("attr").Value); if (Regex.IsMatch(xE.Attribute("attr").EndsWith("AA")) { Console.WriteLine("match"); tuv.Attribute("attr").Value.Replace("AA", ""); } Console.WriteLine(xE.Attribute("attr").Value); } } } catch (Exception e) {