xmlserializer

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

XmlSerializer is throwing InvalidOperationException when using the generic type constraint where

蓝咒 提交于 2019-12-23 08:33:49
问题 When I try to run the following code (two separated assemblies) ClassLibrary.cs public interface ITest { } Program.cs using System; public class TestClass { public void Test<T>(T x) where T : ITest { } } static class Program { static void Main(string[] args) { new System.Xml.Serialization.XmlSerializer(typeof(TestClass)); } } Compiled in Windows 7 64-Bit using the following commands: c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc /target:library ClassLibrary.cs c:\Windows\Microsoft.NET

How to generate tag prefixes using XmlSerializer

为君一笑 提交于 2019-12-23 06:51:21
问题 I wanted to generate the following using XmlSerializer : <atom:link href="http://dallas.example.com/rss.xml" rel="self" type="application/rss+xml" /> So I tried to add a Namespace to my element : [...] [XmlElement("link", Namespace="atom")] public AtomLink AtomLink { get; set; } [...] But the output is : <link xmlns="atom" href="http://dallas.example.com/rss.xml" rel="self" type="application/rss+xml" /> So what is the correct way to generate prefixed tags ? 回答1: First off, the atom namespace

Xmlserializer to C# object, store original XML element

感情迁移 提交于 2019-12-23 03:38:17
问题 Is it possible to store the original XML element in a C# class, for example? Original XML: <data someattributea="" someattributeb="" someattributec="" /> C# using System; using System.Xml.Serialization; using System.Collections.Generic; namespace Xml2CSharp { [XmlRoot(ElementName="data")] public class Data { [XmlAttribute(AttributeName="someattributea")] public string Someattributea { get; set; } [XmlAttribute(AttributeName="someattributeb")] public string Someattributeb { get; set; }

WCF Data Contract / Serialization

流过昼夜 提交于 2019-12-22 08:37:14
问题 I created a simple WCF application which expose one operation. This operation takes a composite data type as parameter. I have not decorated this composite data type with [DataContract] attribute. But this is working and I can see the Schema for this in WSDL. Now my understanding is that this new custom type should be decorated with [Serializable] or [dataContract] attribute to take part in the Web services operation. What I am missing here? 回答1: POCO support have been introduced in WCF since

Cannot Deserialize a SOAP Message programmatically

狂风中的少年 提交于 2019-12-22 08:09:58
问题 I get the following exception when I try to deserialize a soap message. I am doing it this way cause I have the response files I want to reuse in testing. I cannot use a real service or the like as it does not fit the architecture for the testing framework we have. Test 'MyUnitTestMethod' failed: System.InvalidOperationException : There is an error in XML document (1, 2). ----> System.InvalidOperationException : <MySpecialResponse xmlns='http://xsd.com/msgs/v1'> was not expected. at System

OutOfMemoryError calling XmlSerializer.Deserialize() - not related to XML size!

折月煮酒 提交于 2019-12-22 04:36:09
问题 This is a really crazy bug. The following is throwing an OutOfMemoryException , for XML snippits that are very short and simple (e.g., <ABC def='123'/> ): public static T DeserializeXmlNode<T>(XmlNode node) { try { return (T)new XmlSerializer(typeof(T)) .Deserialize(new XmlNodeReader(node)); } catch (Exception ex) { throw; // just for catching a breakpoint. } } I read in this MSDN article that if I were using XmlSerializer with additional parameters in the constructor, I'd end up generating

OutOfMemoryError calling XmlSerializer.Deserialize() - not related to XML size!

孤者浪人 提交于 2019-12-22 04:36:08
问题 This is a really crazy bug. The following is throwing an OutOfMemoryException , for XML snippits that are very short and simple (e.g., <ABC def='123'/> ): public static T DeserializeXmlNode<T>(XmlNode node) { try { return (T)new XmlSerializer(typeof(T)) .Deserialize(new XmlNodeReader(node)); } catch (Exception ex) { throw; // just for catching a breakpoint. } } I read in this MSDN article that if I were using XmlSerializer with additional parameters in the constructor, I'd end up generating

XmlSerializer.Deserialize on a List<> item

时间秒杀一切 提交于 2019-12-22 01:27:52
问题 I've tried all the solutions I could find on SO and elsewhere, but can't seem to figure out why this is not working. Straightforward deserialization of an XML string into an object, the object has one property - a List: [XmlTypeAttribute(AnonymousType = true)] public class UpdateData { [XmlArrayItem(ElementName = "Updates")] public List<Update> Updates { get; set; } public UpdateData() { Updates = new List<Update>(); } } public class Update { [XmlElement(ElementName = "MemberID")] public int

InvalidCastException thrown after install on new machine

夙愿已清 提交于 2019-12-22 00:34:34
问题 I've been using Visual Studio 2010, C#. I've been working on this program for almost a year now. In it, I'm using information stored in separate XML files to build images. This process has worked for me hundreds of times without a problem. I have migrated to a new machine. The previous machine was 32 bit, the new one is 64 bit. When I open a file and attempt to deserialize, the following exception is thrown: System.InvalidCastException was unhandled Message=[A]System.Collections.Generic.List