xelement

How to serialize object with List inside in C# using XElement?

半城伤御伤魂 提交于 2019-12-23 12:13:10
问题 I have object with members of different types like this: public class MyObject { public string Str1 = string.Empty; public MyEnums.Enum1 E1 = MyEnums.Enum1.Unknown; public bool Done = false; }; I have Dictionary of these objects: Dictionary<string, MyObject> MyObjectsDic = new Dictionary<string, MyObject>(); And serializer for it like this: public static void ToXml(string file, string collectionName, Dictionary<string, object> collection) { XElement root = new XElement(collectionName); root

How to convert from string to XElement object

假如想象 提交于 2019-12-23 06:44:47
问题 I have a string like this: "<Root><Child>Hey</Child></Root>" How can I convert this to an XElement Object? 回答1: Use XElement.Parse method like below XElement xmlTree = XElement.Parse("<Root><Child>Hey</Child></Root>"); Console.WriteLine(xmlTree); 来源: https://stackoverflow.com/questions/7847424/how-to-convert-from-string-to-xelement-object

Innertext from XElement? [duplicate]

江枫思渺然 提交于 2019-12-22 05:13:37
问题 This question already has answers here : Best way to get InnerXml of an XElement? (15 answers) Closed 5 years ago . I'm having a hard time getting the correct value from the innertext of an XElement. First, here's the XML that I'm using. This is a copy of our production data that results from a process in our workflow. In other words, I can't change the XML, I can only parse it. The element whose innertext I'd like to get has data inside that looks like XML, but it isn't. It is straight text

Convert single XElement to object

*爱你&永不变心* 提交于 2019-12-22 01:46:06
问题 I have a single XElement looking like this: <row flag="1" sect="" header="" body="" extrainfo="0" /> Then I have a class looking like this: public class ProductAttribute { public string Flag { get; set; } public string Sect { get; set; } public string Header { get; set; } public string Body { get; set; } public string Extrainfo { get; set; } } How can I convert this XElement into a ProductAttribute object? 回答1: You have to put the correct serialization attributes on your class and class

How to parse single xml node in windows phone

无人久伴 提交于 2019-12-20 07:08:48
问题 <root>1234</root> how do I pasrse this single xml node and it comes from web service response, I am using: void webservice_completedeventArgs() { XDocument doc = XDocument.Parse(e.Result); } this code runs very well in Emulator but in device when I debug, I am getting this error: "Data at Root level is invalid" I think its happening due to, There is no XML Dclaration in web service response like "<XML Version 1.0 Encoding UTF-8?>" So how do I parse that single XML node. Please any help would

Add the XAttribute to XElement if attribute exists in the element

*爱你&永不变心* 提交于 2019-12-20 04:37:37
问题 Need to add XAttribute newatt = new XAttribute("TAG", value); to XElement elem , but the elem could already contain the attribute with the name "TAG" , so the elem.Add(newatt); would give error. The workaround I use at the moment is to check first: if (elem.Attribute("TAG") != null) // check if attribute exists elem.SetAttributeValue("TAG", newatt.Value.ToString()); // Attribute exists else elem.Add(newatt); // Attribute does not exist Is there a shorter way to do this task, perhaps already

Object reference not set to an instance of an object. Trying to put XML into a List

浪子不回头ぞ 提交于 2019-12-20 04:16:58
问题 I have to following XML code witch I like to convert into a List with keys and values: <?xml version='1.0' encoding='UTF-8' standalone='no'?> <root> <command>getClient</command> <id>10292</id> </root> My C# code is like this: XElement aValues = XElement.Parse(sMessage); List<KeyValuePair<string, object>> oValues = aValues.Element("root").Elements().Select(e => new KeyValuePair<string, object>(e.Name.ToString(), e.Value)).ToList(); sMessage is the XML string. Now I'm getting the following

Linq To Xml problems using XElement's method Elements(XName)

百般思念 提交于 2019-12-19 09:27:23
问题 I have a problem using Linq To Xml. A simple code. I have this XML: <?xml version="1.0" encoding="utf-8" ?> <data xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/directory file.xsd"> <contact> <name>aaa</name> <email>email@email.ext</email> <birthdate>2002-09-22</birthdate> <telephone>000:000000</telephone> <description>Description for this contact</description> </contact> <contact> <name>sss</name> <email>email

Xml Linq, removing duplicate nodes in XElement C#

↘锁芯ラ 提交于 2019-12-19 05:07:14
问题 I use Xml.Linq for manage xml configuration files. I have XElement (Company.CalidadCodigo.ParserSQL.Reglas), and I need remove duplicate values in XElement (nodes Add-Key-Value, which Value is repeated). I use Union but not right. var reglasComunes = reglasParaTarget.Union(reglasParaSecundario); Any sample code about it? <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="Company.CalidadCodigo.ParserSQL.Reglas" type="System.Configuration

How to best detect encoding in XML file?

余生长醉 提交于 2019-12-18 05:54:36
问题 To load XML files with arbitrary encoding I have the following code: Encoding encoding; using (var reader = new XmlTextReader(filepath)) { reader.MoveToContent(); encoding = reader.Encoding; } var settings = new XmlReaderSettings { NameTable = new NameTable() }; var xmlns = new XmlNamespaceManager(settings.NameTable); var context = new XmlParserContext(null, xmlns, "", XmlSpace.Default, encoding); using (var reader = XmlReader.Create(filepath, settings, context)) { return XElement.Load(reader