Tag: xmlserializer

将Class转换为XML为string

我正在使用XMLSerializer将类序列化为XML。 有很多例子可以将XML保存到文件中。 但是我想要的是将XML放入字符串而不是将其保存到文件中。 我正在尝试下面的代码,但它不起作用: public static void Main(string[] args) { XmlSerializer ser = new XmlSerializer(typeof(TestClass)); MemoryStream m = new MemoryStream(); ser.Serialize(m, new TestClass()); string xml = new StreamReader(m).ReadToEnd(); Console.WriteLine(xml); Console.ReadLine(); } public class TestClass { public int Legs = 4; public int NoOfKills = 100; } 有想法该怎么解决这个吗 ? 谢谢。

C#xml序列化

我在c#中将对象序列化为xml,我想序列化 public String Marker { get; set; } 成 当字符串标记没有值时。 现在我明白了 for Marker == string.Empty ,没有标记节点为null 。 我怎么能得到这个?

将xml反序列化为类,遇到列表的问题

我有以下XML 我正在尝试编写可以将其反序列化的类,这就是我所拥有的: [XmlRoot(“map”)] public class MyMap { [XmlAttribute(“version”)] public decimal Version { get; set; } [XmlElement(“properties”)] public List Properties { get; set; } } public class MyProperty { [XmlAttribute(“name”)] public string Name { get; set; } [XmlAttribute(“value”)] public string Value { get; set; } } 问题是我似乎无法读取属性列表,我只得到一个条目,它在Name和Value中都为null。 我需要设置一些神奇的属性来使其工作吗?

XmlSerializer的。 跳过xml未知节点

我的xml文件反序列化有问题。 让我们假设我们有一个xml文件和一个我们用于反序列化的类。 例如: xml – newDataStore1 sdffasdfasdf Shapefile false newTestWorkspace false true ISO-8859-1 shapefile true file:data/shapefiles/states.shp http://www.opengeospatial.net/cite false 类 namespace GeoServerApiTester { /// [System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Xml”, “4.0.30319.225”)] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute(“code”)] [System.Xml.Serialization.XmlRootAttribute(“dataStore”, Namespace=””, IsNullable=false)] public partial class DataStore { private string nameField; private string typeField; private bool enabledField; private WorkSpacePreview workspaceField; private ConnectionParametersStorageEntryCollection connectionParametersField; private string @__defaultField; private […]

当使用generics类型约束时,XmlSerializer抛出InvalidOperationException

当我尝试运行以下代码(两个分离的程序集)时 ClassLibrary.cs public interface ITest { } Program.cs中 using System; public class TestClass { public void Test(T x) where T : ITest { } } static class Program { static void Main(string[] args) { new System.Xml.Serialization.XmlSerializer(typeof(TestClass)); } } 使用以下命令在Windows 7 64位中编译: c:\ Windows \ Microsoft.NET \ Framework \ v2.0.50727 \ csc / target:library ClassLibrary.cs c:\ […]

以UTF-16编码格式反序列化xml文件时出现XmlException

使用C#的XmlSerializer。 在反序列化给定文件夹中的所有xml文件的过程中,我看到XmlException “There is an error in XML document (0, 0)”. 和InnerException是”There is no Unicode byte order mark. Cannot switch to Unicode”. 目录中的所有xmls都是“UTF-16”编码的。 唯一不同的是,一些xml文件缺少在反序列化时我正在使用的对象类中定义的元素。 例如,考虑我的文件夹中有3种不同类型的xmls: file1.xml file2.xml file3.xml 我有一个类代表上面的xml: [XmlTypeAttribute(AnonymousType = true, Namespace = “http://my.PaymentStatus”)] [XmlRootAttribute(“PaymentStatus”, Namespace = “http://http://my.PaymentStatus”, IsNullable = true)] public class PaymentStatus { private PaymentStatus2[] PaymentStatus2Field; [XmlElementAttribute(“PaymentStatus2”, Namespace = “”)] public PaymentStatus2[] […]

如何使用XmlSerializer生成标记前缀

我想使用XmlSerializer生成以下内容: 所以我尝试在我的元素中添加一个命名空间: […] [XmlElement(“link”, Namespace=”atom”)] public AtomLink AtomLink { get; set; } […] 但输出是: 那么生成前缀标签的正确方法是什么?

为什么XmlSerializer的Deserialize()吐出一个子对象,这是一个XmlNode ?

我正在使用XmlSerializer序列化然后反序列化一个简单的对象。 当我反对反序列化对象时,我发现一个子对象没有被正确地反序列化,而是变成了XmlNode[] 。 这里几乎是我得到的结构: // This line I put in here as a way of sneaking into the XML the // root node’s C# namespace, since it’s not the same as the // deserializing code and the deserializing code seemed unable to // deserialize properly without knowing the Type (see my code below). // So […]

按特定顺序将并行数组序列化为C#中的XML

我目前的任务是将数据发送到具有指定列表的奇怪方式的Web服务。 我无法控制架构,我尝试让另一方更改架构失败了。 所以我几乎坚持这一点。 它们的模式定义方式是这样的(仅包含相关位): 我使用xsd.exe生成一个C#类来轻松地序列化结构。 生成的位如下: [System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”, “4.0.30319.33440”)] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute(“code”)] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace=”the namespace”)] public partial class PartList { [System.Xml.Serialization.XmlElementAttribute(“Name”, Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string[] Name { get; set; } [System.Xml.Serialization.XmlElementAttribute(“Data”, Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string[] Data { get; set; } [System.Xml.Serialization.XmlElementAttribute(“OtherData”, Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string[] OtherData { get; set; } } 是的,并行数组。 现在,根据他们的文档(以及来自通过其他方式生成XML的另一方的轶事数据),正确/预期的xml应该如下所示(例如,包含两个项目的列表 – 为了说明目的添加注释内联): Some Test 123131313 0.11 […]

无法使用XmlSerializer读取XML内容

我在课堂上得到了这个: namespace MSAToolsLibrary.PublisherEntry { [XmlRoot(ElementName = “PublisherDatabase”, Namespace = “http://www.publictalksoftware.co.uk/msa”)] public class PublisherData { public PublisherData() { //_Publishers = new List(); _PublishersDictionary = new Dictionary(); } public List Publishers { get { return _PublishersDictionary.Select(x => x.Value).ToList(); } set { _PublishersDictionary = value.ToDictionary(x => x.Name, x => x); } } private Dictionary _PublishersDictionary; [XmlIgnore] public Dictionary […]