linq-to-xml

Xdocument does not print declaration

本小妞迷上赌 提交于 2019-12-10 00:55:21
问题 I try to use the domainpeople.com API and to do I need to use XML. Currently I have an error saying "apiProtocol is not found" I guess then that my Xml document is malformed. The Current xml sent is : <apiProtocol version="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNameSpaceSchemaLocation="checkrequest.xsd"> <checkRequest user="ifuzion" password="fish4gold121" reference="123456789"> <domain name="google.com" /> </checkRequest> </apiProtocol> Apparently the <?xml?> part

Convert XElement to string

情到浓时终转凉″ 提交于 2019-12-10 00:35:17
问题 I have a simple XElement object XElement xml = new XElement("XML", new XElement ("TOKEN",Session["Token"]), new XElement("ALL_INCLUSIVE", "0"), new XElement("BEACH", "0"), new XElement("DEST_DEP", ddlDest.SelectedValue.ToString()), new XElement("FLEX", "0") ); Where want to dump out the contents into a string. Exactly like how Console.Writeline(xml); does, but I want the contents in a string. I tried various methonds. xml.ToString(); doesn't return anything on its own. 回答1: ToString should

Parse xml in c# : combine xmlreader and linq to xml

浪子不回头ぞ 提交于 2019-12-09 23:00:37
问题 I have to parse large XML file in C#. I use LINQ-to-XML. I have a structure like <root> <node></node> <node></node> </root> I would like use XmlReader to loop on each node and use LINQ-to-XML to get each node and work on it ? So I have only in memory the current node. 回答1: You can do something like that: string path = @"E:\tmp\testxml.xml"; using (var reader = XmlReader.Create(path)) { bool isOnNode = reader.ReadToDescendant("node"); while (isOnNode) { var element = (XElement)XNode.ReadFrom

C# Generic List conversion to Class implementing List<T>

依然范特西╮ 提交于 2019-12-09 20:54:05
问题 The following code produces the issue Cannot implicitly convert type 'System.Collections.Generic.IEnumberable<DataField<string>>' to 'DataFields'. An explicit conversion exists (are you missing a cast?). How do I get around this? What am I doing wrong? public class DataFields : List<DataField> { } public abstract class DataField { public string Name { get; set; } } public class DataField<T> : DataField { public T Value { get; set; } } public static DataFields ConvertXML(XMLDocument data) {

XElement and List<T>

我的未来我决定 提交于 2019-12-09 16:50:13
问题 I have a class that has the following properties: public class Author { public string FirstName { get; set; } public string LastName { get; set; } } Next, I have a List of Authors like so: List<Author> authors = new List<Author> (); authors.add( new Author { FirstName = "Steven", LastName = "King" }); authors.add( new Author { FirstName = "Homer", LastName = "" }); Now, I am trying to use Linq to XML in order to generate the XML representing my Authors List. new XElement("Authors", new

Get UTF-8 in Uppercase using XDocument

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 16:41:03
问题 I need to have XML encoding and version at the top of my XML document which I am making with XDocument . I have this but it is in lowercase, and it needs to be in uppercase. What do I need to do? I declare a new XML document using the XDocument class called 'doc'. I save this to a file using doc.Save(); . I have tried: doc.Declaration.Encoding.ToUpper(); Declaring a new XDeclaration Typing the Encoding in uppercase and setting my doc.Declaration to my XDeclaration . It still comes through in

Prevent XmlTextReader from expanding entities

非 Y 不嫁゛ 提交于 2019-12-09 15:55:31
问题 I am trying to read a XML document without expanding the entities, do some manipulations to it, and re-save it with the unexpanded entities as they were initially. When using the XDocument directly, it fails to load, throwing an exception tell me it has unexpanded entities: XDocument doc = XDocument.Load(file); // <--- Exception // ... do some manipulation to doc doc.Save(file2); Exception: Reference to undeclared entity 'entityname'. Then I tried to pass the XmlTextReader to the XDocument

How do I have to change this XML string so that XDocument.Parse reads it in?

五迷三道 提交于 2019-12-09 15:12:13
问题 In the following code, I serialize an object into an XML string . But when I try to read this XML string into an XDocument with XDocument.Parse, it gives me this error : Invalid data at root level. The XML is: <?xml version="1.0" encoding="utf-8"?> <Customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Id>1</Id> <FirstName>Jim</FirstName> <LastName>Jones</LastName> <ZipCode>23434</ZipCode> </Customer> UPDATE: Here is the hex: ![alt text]

Where is my XDeclaration?

我的梦境 提交于 2019-12-09 14:19:50
问题 For some reason the following code produces XML that does not contain a declaration: XDocument xDocument = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement("project", new XAttribute("number", project.ProjectNumber), new XElement("store", new XAttribute("number", project.StoreNumber) ), // User Element new XElement("user", new XAttribute("owner-id", project.OwnerID ?? 0), new XElement("email", new XCData(project.OwnerEmail ?? "")), new XElement("project-name", new XCData

Convert XDocument to Stream

柔情痞子 提交于 2019-12-08 19:18:54
问题 How do I convert the XML in an XDocument to a MemoryStream, without saving anything to disk? 回答1: Have a look at the XDocument.WriteTo method; e.g.: using (MemoryStream ms = new MemoryStream()) { XmlWriterSettings xws = new XmlWriterSettings(); xws.OmitXmlDeclaration = true; xws.Indent = true; using (XmlWriter xw = XmlWriter.Create(ms, xws)) { XDocument doc = new XDocument( new XElement("Child", new XElement("GrandChild", "some content") ) ); doc.WriteTo(xw); } } 回答2: In .NET 4 and later, you