linq-to-xml

Create or replace node in an XML without root in C#

大兔子大兔子 提交于 2021-02-20 18:55:46
问题 I have an XML file like this: <Something>....</Something> <Another>....</Another> <Other>...</Other> This XML file does not have a root element (I know that this is a wrong XML format). I need to create or replace (if it already exists) a node in this XML, but I can't work with XDocument or XmlDocument because they need a root element to work, and I can't add a root element to this XML because I can't change more code in the Application (Windows forms application). What are my options to

Create or replace node in an XML without root in C#

好久不见. 提交于 2021-02-20 18:54:12
问题 I have an XML file like this: <Something>....</Something> <Another>....</Another> <Other>...</Other> This XML file does not have a root element (I know that this is a wrong XML format). I need to create or replace (if it already exists) a node in this XML, but I can't work with XDocument or XmlDocument because they need a root element to work, and I can't add a root element to this XML because I can't change more code in the Application (Windows forms application). What are my options to

Create or replace node in an XML without root in C#

一笑奈何 提交于 2021-02-20 18:54:03
问题 I have an XML file like this: <Something>....</Something> <Another>....</Another> <Other>...</Other> This XML file does not have a root element (I know that this is a wrong XML format). I need to create or replace (if it already exists) a node in this XML, but I can't work with XDocument or XmlDocument because they need a root element to work, and I can't add a root element to this XML because I can't change more code in the Application (Windows forms application). What are my options to

XDocument select distinct elements using LINQ

你离开我真会死。 提交于 2021-02-19 08:15:31
问题 I am trying to get a distinct list of elements but it always returns everything. Xml: <root> <row> <unit>CAN</unit> </row> <row> <unit>KG</unit> </row> <row> <unit>KG</unit> </row> <row> <unit>PKT</unit> </row> <row> <unit>CAN</unit> </row> <row> <unit>PKT</unit> </row> <row> <unit>KG</unit> </row> </root> Linq: List<XElement> elements = (from e in xdoc.Descendants("row").Elements() where e.Name.Equals("unit") select e).Distinct().ToList(); Expected output: elements list should contain 3

Get a xml element with specific attribute value in c#

情到浓时终转凉″ 提交于 2021-02-19 05:38:05
问题 I need to get a value of a SubTopic element which has an attribute called "Name" with specific value. I do it this way; IEnumerable<XElement> list = (from el in xdoc.Elements() where (string)el.Attribute("Name") == "creatingTests" select el); The collection has zero elements. I tried putting xdoc.Elements("SubTopic") instead of empty parameter, but with no success. My XML file structure; <?xml version="1.0" encoding="windows-1250" ?> <Help Title="TestTool - tematy pomocy"> <Topic Name=

Delete Attribute of Xml node c#

送分小仙女□ 提交于 2021-02-11 15:06:37
问题 I have a XML file like this: <?xml version="1.0" encoding="utf-8"?> <ApplicationConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ua="http://opcfoundation.org/UA/2008/02/Types.xsd" xmlns="http://opcfoundation.org/UA/SDK/Configuration.xsd"> <ServerConfiguration> <SecurityPolicies> <ServerSecurityPolicy> <SecurityMode>None_1</SecurityMode> </ServerSecurityPolicy> </SecurityPolicies> </ServerConfiguration> </ApplicationConfiguration> What I want is to add more node named

How can I move an XML element above the previous element using LINQ to XML?

て烟熏妆下的殇ゞ 提交于 2021-02-08 09:41:38
问题 I have the following XML structure: <?xml version="1.0" encoding="utf-8" standalone="yes"?> <Root xmlns:xsi="My Program"> <NotRoot Text="Hello"> <SomeOption Text="Option 1" Centered="False"> <SomeOption Text="Option 1.1" Centered="False"> <SomeOption Text="Option 1.1.1" Centered="false"> <SomeOption Text="A" Centered="false"> <SpecialName Text="Blah blah" Centered="false"> <Number>1</Number> </SpecialName> </SomeOption> <SomeOption Text="B" Centered="false"> <SpecialName Text="Hi" Centered=

Difference between XPathEvaluate on XElement or XDocument?

孤街醉人 提交于 2021-02-07 20:42:54
问题 Somewhere in a C# program, I need to get an attribute value from an xml structure. I can reach this xml structure directly as an XElement and have a simple xpath string to get the attribute. However, using XPathEvaluate, I get an empty array most of the time. (Yes, sometimes, the attribute is returned, but mostly it isn't... for the exact same XElement and xpath string...) However, if I first convert the xml to string and reparse it as an XDocument, I do always get the attribute back. Can

Difference between XPathEvaluate on XElement or XDocument?

杀马特。学长 韩版系。学妹 提交于 2021-02-07 20:40:50
问题 Somewhere in a C# program, I need to get an attribute value from an xml structure. I can reach this xml structure directly as an XElement and have a simple xpath string to get the attribute. However, using XPathEvaluate, I get an empty array most of the time. (Yes, sometimes, the attribute is returned, but mostly it isn't... for the exact same XElement and xpath string...) However, if I first convert the xml to string and reparse it as an XDocument, I do always get the attribute back. Can

Load XDocument asynchronously

佐手、 提交于 2021-02-07 05:51:24
问题 I want to load large XML documents into XDocument objects. The simple synchronous approach using XDocument.Load(path, loadOptions) works great, but blocks for an uncomfortably long time in a GUI context when loading large files (particularly from network storage). I wrote this async version with the intention of improving responsiveness in document loading, particularly when loading files over the network. public static async Task<XDocument> LoadAsync(String path, LoadOptions loadOptions =