xmlserializer

Deserialize XML string to Object Error : There is an Error in xml document (1,2)

假装没事ソ 提交于 2019-12-05 07:05:32
From windows event viewer I can get the following xml structure: <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event"> <System> <Provider Name="XXXXXXXXXX" Guid="{YYYYYYYY}" /> <EventID>XYZ</EventID> <Version>0</Version> <Level>L</Level> <Task>A</Task> <Opcode>0</Opcode> <Keywords>0x000xyzh</Keywords> <TimeCreated SystemTime="2012-06-28T15:44:04.997837000Z" /> <EventRecordID>153</EventRecordID> <Correlation ActivityID="{DDDDDDDDD}" /> <Execution ProcessID="199999" ThreadID="90990" /> <Channel>Microsoft-Windows-ABCDEFG/Admin</Channel> <Computer>myPC</Computer> <Security UserID=

Serialize a polymorphic List with the same type name

笑着哭i 提交于 2019-12-05 05:14:40
I have an object I'm trying to serialize into XML. Inside this object is a list of a generic type (abstract class). Each item in this list could be a different class, but all are inheriting from the abstract base class: public abstract class animal { public string type { get; set; } } public class cat:animal { public string size { get; set; } public string furColor { get; set; } } public class fish:animal { public string size { get; set; } public string scaleColor { get; set; } } When I serialize the list, I want it to look like this: <animal type="cat"> <size>medium</size> <furColor>black<

Performance: BinaryFormatter vs. XmlSerializer

匆匆过客 提交于 2019-12-05 02:39:05
I read very often that the BinaryFormatter has better performance then XmlSerializer. Out of curiosity, I wrote a test-app. a wtf moment... why is Xml so much faster than Bin (especially the deserialization)? using System; using System.Collections.Generic; using System.Runtime.Serialization; using System.Xml.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.IO; namespace SerPlayground { class Program { static void Main(string[] args) { var items = new List<TestClass>(); for (int i = 0; i < 1E6; i++) { items.Add(new TestClass() { Name = i.ToString(), Id = i }); }

XmlSerializer.Deserialize on a List<> item

陌路散爱 提交于 2019-12-05 00:14:22
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 MemberID { get; set; } [XmlElement(ElementName = "AnalysisID")] public int AnalysisID { get; set; }

WCF: provide generic FaultException in IErrorHandler

北城余情 提交于 2019-12-04 21:54:33
问题 Some context: We have a custom XSD and generate the WSDL and C# code using WSCF.blue. The client side uses ChannelFactory<T> and shares the interface which includes all the attributes added by WSCF.blue to match what is in the XSD. I'm trying to implement IErrorHandler.ProvideFault where it provides a generic FaultException<T> , but on the client side I'm getting back a non-generic FaultContract . This is what my ProvideFault method looks like: public void ProvideFault(Exception error,

InvalidCastException thrown after install on new machine

六月ゝ 毕业季﹏ 提交于 2019-12-04 18:04:50
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 1[BookCreator.pageFontInfo] cannot be cast to [B]System.Collections.Generic.List 1[BookCreator

XmlSerializer stopped working after updates

元气小坏坏 提交于 2019-12-04 17:25:52
I'm using XmlSerializer. I've had no problems with it until now. I updated Silverlight from 4 to 5 and at the same time also updated the WCF RIA Services from v1 SP1 to v1 SP2. Now the following line gives me an error. XmlSerializer s = new XmlSerializer(typeof(MyCustomObject)); The error is: System.InvalidOperationException: System.ServiceModel.DomainServices.Client.EntityConflict cannot be serialized because it does not have a parameterless constructor. The object I'm using (MyCustomObject in the sample) has not changed in any way so I'm starting to think it's either SL5 or the new RIA

Using XmlAttributeOverrides on Nested Properties

旧巷老猫 提交于 2019-12-04 15:53:32
I am trying to use XmlAttributeOverrides to control which class properties appear in the xml after the class has been serialized. It works on properties that are on the "root" class but not on nested properties. Here is a simple example to illustrate what I'm trying to accomplish. My class hierarchy is as follows: public class Main { public string Name { get; set; } public Location Address { get; set; } } public class Location { public string StreetAddress { get; set; } public Contact ContactInfo{ get; set; } } public class Contact { public string PhoneNumber { get; set; } public string

C# System.Xml.Serialization Self-nested elements

给你一囗甜甜゛ 提交于 2019-12-04 12:34:01
I am trying to deserialize <graph> <node> <node> <node></node> </node> </node> <node> <node> <node></node> </node> </node> </graph> with [XmlRoot("graph")] class graph { List<Node> _children = new List<node>(); [XmlElement("node")] public Node[] node { get { return _children.ToArray(); } set { foreach(Node n in value) children.add(n) } }; } class Node { List<Node> _children = new List<node>(); [XmlElement("node")] public Node[] node { get { return _children.ToArray(); } set { foreach(Node n in value) children.add(n) } }; } but it keeps saying object not created, null reference encountered when

How to deserialize a node in a large document using XmlSerializer

偶尔善良 提交于 2019-12-04 05:31:57
问题 I have a large XML document that I have loaded into an XmlDocument and I want to use the XmlSerializer class to deserialize selected elements from it into a .NET class generated using xsd.exe. Here's an MCVE of what I've tried so far; the xsd and generated class are at the end of the post. As noted in the comments in the code, I am getting an InvalidOperationException - <Cars xmlns:'http://MyNamespace' /> was not expected : static string XmlContent = @" <RootNode xmlns=""http://MyNamespace"">