linq-to-xml

Parsing a string in c#

北慕城南 提交于 2019-12-23 13:16:05
问题 Suppose there is an xml file like below: <Instances> <Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image1.jpg" ImageNumber = "1"/> <Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image2.jpg" ImageNumber = "2"/> <Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg" Location="C:\Series1\Image3.jpg" ImageNumber = "3"/> <Bits = "16" XCoord = "64" YCoord = "64" ZCoord = "64" FileType="jpeg"

Disposing an XDocument object

孤人 提交于 2019-12-23 10:16:32
问题 How do I "dispose" an XDocument object? I am using it to parse an XML string and then save the file to the file system. In the same method I then need to access this file and run a command line tool on it. The code is as follows: string filepath = "..."; string filename = "..."; XDocument xdoc = XDocument.Parse(xmlString); xdoc.Save(filepath + filename); Process p = Process.Start(new ProcessStartInfo("rst.exe", args)); // the args use the file saved above I put a breakpoint on the line where

XDocument to string: How to omit encoding in declaration?

空扰寡人 提交于 2019-12-23 10:01:39
问题 I'm writing a wrapper for a braindead enterprise XML API. I have an XDocument that I need to turn into a string. Due to the fact that their XML parser is so finicky that it cannot even handle whitespace between XML nodes, the document declaration MUST be EXACTLY: <?xml version="1.0"?> However, the XDocument.Save() method always adds an encoding attribute in that declaration: <?xml version="1.0" encoding="utf-16"?> With the past hour spent on Google and Stack looking for the best way to

Force XDocument to not use namespace prefix if namespace is also defined as default

与世无争的帅哥 提交于 2019-12-23 09:57:10
问题 I have an xml file with default namespace specified with and without namespace prefix. When I generate xml output I'm getting all xml elements prefixed. Is there way to get rid of the prefixes since I'm using the default namespace? class Program { static void Main(string[] args) { var xml = "<root xmlns='default-namespace' xmlns:key='default-namespace'>" + " <node1></node1>" + " <node2></node2>" + "</root>"; var document = XDocument.Parse(xml); var output = document.ToString(); } } The output

Performance: XmlSerializer vs XmlReader vs XmlDocument vs XDocument

蹲街弑〆低调 提交于 2019-12-23 09:38:42
问题 I'm working on a little web project and would like to read/write to an XML file. Performance is my first priority. I've come to this great post on comparing the mentioned approaches except XmlSerializer . I prefer XmlSerializer since it makes the code much cleaner. But I don't know about its performance. What kind does XmlSerializer use inside to write to XML files? 回答1: As for the performance of XmlSerializer, see http://msdn.microsoft.com/en-us/library/182eeyhh.aspx which says: The

Parsing HTML document: Regular expression or LINQ?

∥☆過路亽.° 提交于 2019-12-23 07:54:37
问题 Trying to parse an HTML document and extract some elements (any links to text files). The current strategy is to load an HTML document into a string. Then find all instances of links to text files. It could be any file type, but for this question, it's a text file. The end goal is to have an IEnumerable list of string objects. That part is easy, but parsing the data is the question. <html> <head><title>Blah</title> </head> <body> <br/> <div>Here is your first text file: <a href="http:/

Is there a way to serialize multiple XElements onto the same line?

放肆的年华 提交于 2019-12-23 07:04:30
问题 I'm dealing with the dreaded <Run/> in Silverlight 3 and having to programmatically create a <TextBlock> and its inlines: Why dreaded? Because it doesn't work, I guess, the way you'd expect. Exhibit A, below, should produce BARN (with fancy colors for each character), but instead it produces: B A R N EXHIBIT A <TextBlock FontFamily="Comic Sans MS" FontSize="88"> <Run Foreground="#A200FF">B</Run> <Run Foreground="#FF0000">A</Run> <Run Foreground="#FFC000">R</Run> <Run Foreground="#FFFF00">N<

Is there a way to serialize multiple XElements onto the same line?

徘徊边缘 提交于 2019-12-23 07:04:29
问题 I'm dealing with the dreaded <Run/> in Silverlight 3 and having to programmatically create a <TextBlock> and its inlines: Why dreaded? Because it doesn't work, I guess, the way you'd expect. Exhibit A, below, should produce BARN (with fancy colors for each character), but instead it produces: B A R N EXHIBIT A <TextBlock FontFamily="Comic Sans MS" FontSize="88"> <Run Foreground="#A200FF">B</Run> <Run Foreground="#FF0000">A</Run> <Run Foreground="#FFC000">R</Run> <Run Foreground="#FFFF00">N<

How do I add an XElement to a document, avoiding the “incorrectly structured document” error?

一笑奈何 提交于 2019-12-23 06:48:54
问题 // Remove element with ID of 1 var userIds = from user in document.Descendants("Id") where user.Value == "1" select user; userIds.Remove(); SaveAndDisplay(document); // Add element back var newElement = new XElement("Id", "0", new XElement("Balance", "3000")); document.Add(newElement); SaveAndDisplay(document); The add element back block is the problem. As when it gets to the add it states: This operation would create an incorrectly structured document. What stupid mistake am I making? Edit:

VB.NET XML Literal Expression Expected with Linq to XML

允我心安 提交于 2019-12-23 06:31:32
问题 I have the same problem as stated in this question, but the accepted solution there was a "works on my machine" answer. Here is my code: Dim document As XDocument = _ <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <GetUser xmlns="http://foo.com/bar.asmx"> <encryptedHash>HashString</encryptedHash> <accessKey