Tag: xml serialization

直接/从XML Linq序列化

有没有办法取消/序列化一个对象而不绕过XmlDocument / temp字符串? 我正在寻找以下内容: class Program { static void Main(string[] args) { XDocument doc = new XDocument(); MyClass c = new MyClass(); c.SomeValue = “bar”; doc.Add(c); Console.Write(doc.ToString()); Console.ReadLine(); } } [XmlRoot(ElementName=”test”)] public class MyClass { [XmlElement(ElementName = “someValue”)] public string SomeValue { get; set; } } 我这样做时会出现错误(非空格字符无法添加到内容中。)如果我将该类包装在元素中,我看到写入的内容是 ConsoleApplication17.MyClass – 所以错误说得通。 我有自动de / serialize的扩展方法,但这不是我想要的(这是客户端,但我仍然想要更优化的东西)。 有任何想法吗?

XmlInclude:列表和数组

我有一个对象,它有变量作为object ,我想用XML序列化它。 为此,我添加了一些XmlInclude属性,以便管理可以使用的所有类型。 [Serializable] [XmlInclude(typeof(short[]))] [XmlInclude(typeof(ushort[]))] [XmlInclude(typeof(int[]))] [XmlInclude(typeof(uint[]))] [XmlInclude(typeof(ulong[]))] [XmlInclude(typeof(long[]))] [XmlInclude(typeof(byte[]))] [XmlInclude(typeof(decimal[]))] [XmlInclude(typeof(float[]))] [XmlInclude(typeof(double[]))] [XmlInclude(typeof(string[]))] [XmlInclude(typeof(List))] [XmlInclude(typeof(List))] [XmlInclude(typeof(List))] [XmlInclude(typeof(List))] [XmlInclude(typeof(List))] [XmlInclude(typeof(List))] [XmlInclude(typeof(List))] [XmlInclude(typeof(List))] [XmlInclude(typeof(List))] [XmlInclude(typeof(List))] [XmlInclude(typeof(List))] [XmlInclude(typeof(MyObject))] [XmlInclude(typeof(TimeSpan))] [XmlInclude(typeof(OtherObject))] [XmlInclude(typeof(MySubObject1))] [XmlInclude(typeof(MySubObject2))] [XmlRoot(ElementName = “mc”)] public class MyClass: IComparable { [XmlElement(“fm”)] public object FirstMember; [XmlElement(“sm”)] public object SecondMember; [XmlElement(“tm”)] public object ThirdMember; } 我的问题是数组和列表声明不共存。 奇怪的是,如果首先放置数组属性,则数组成员被正确序列化,但不是列表成员。 反之亦然。 […]

我可以创建一个带正斜杠的元素作为名称的一部分

我可以创建一个带正斜杠的元素作为名称的一部分吗? fruit 对我来说似乎无效但只是想确认一下。

在XML序列化期间删除命名空间

鉴于此通用序列化代码: public virtual string Serialize(System.Text.Encoding encoding) { System.IO.StreamReader streamReader = null; System.IO.MemoryStream memoryStream = null; memoryStream = new System.IO.MemoryStream(); System.Xml.XmlWriterSettings xmlWriterSettings = new System.Xml.XmlWriterSettings(); xmlWriterSettings.Encoding = encoding; System.Xml.XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings); Serializer.Serialize(xmlWriter, this); memoryStream.Seek(0, System.IO.SeekOrigin.Begin); streamReader = new System.IO.StreamReader(memoryStream); return streamReader.ReadToEnd(); } 和这个对象(来自xsd2code): [System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Xml”, “4.0.30319.225”)] [System.SerializableAttribute()] [System.ComponentModel.DesignerCategoryAttribute(“code”)] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = “Com.Foo.Request”)] [System.Xml.Serialization.XmlRootAttribute(Namespace […]

使用C#.NET处理XML中的禁用字符

我有一个对象,我正在序列化为xml。 似乎其中一个属性中的值包含hex字符0x1E。 我已经尝试将XmlWriterSettings的Encoding属性设置为“utf-16”和“unicode”,但我仍然会抛出exception: 这是生成XML文档的错误。 —> System.InvalidOperationException:生成XML文档时出错。 —> System.ArgumentException:”,hex值0x1E,是无效字符。 有没有办法让这些字符进入xml? 如果没有,是否还有其他字符会导致问题?

从XML序列化数组中删除包装元素

我使用的是VSTS2008 + C#+ .Net 3.0。 我使用下面的代码来序列化XML,我的对象包含数组类型属性,但是生成了一些其他元素的图层(在我的示例中,MyInnerObject和MyObject),我想从生成的XML文件中删除它们。 有任何想法吗? 当前生成的XML文件, Foo Type 预期的XML文件, Foo Type 目前的代码, public class MyClass { private MyObject[] _myObjectProperty; [XmlArrayItemAttribute(IsNullable=false)] public MyObject[] MyObjectProperty { get { return _myObjectProperty; } set { _myObjectProperty = value; } } } public class MyObject { private MyInnerObject[] _myInnerObjectProperty; [XmlArrayItemAttribute(IsNullable = false)] public MyInnerObject[] MyInnerObjectProperty { get { […]

通过C#中的XmlSerializer类反序列化多个具有相同名称的XML元素

我在表单中有一个XML 0 0 0 1 0 0 0 0 0 480 1020 0 我需要对其进行反序列化,更改其内容并将其保存回来。 我在阅读ScheduledDay元素时遇到问题。 我的class级就像 public class BackupScheduleSettings { public BackupScheduleSettings() { ScheduledDay = new int[7]; } ….. public int[] ScheduledDay { get; set; } ….. } 现在,当我加载具有ScheduledDay正确值的XML内容时,我的类数组仍为NULL。 我无法修改XML的内容/格式,因为它是遗留代码。 我不想使用XDocument读取值,因为它是一个大型XML,我需要再次序列化它。 我没有任何帮助就搜索了很多。 任何想法将受到高度赞赏。 谢谢…

VS2010将System.Xml.XmlElement与System.Xml.Linq.XElement混淆?

我用一个返回System.Xml.XmlElement方法创建了一个WCF服务: 接口: [ServiceContract] public interface IWCFService { [OperationContract] XmlElement Execute(…); } 服务: public XmlElement Execute(…) { XmlNode node = …; return (XmlElement)node; } 当我尝试访问我服务器上部署的服务时 WCFServiceClient service = new WCFServiceClient(“WSHttpBinding_IWCFService”); XmlElement node = service.Execute(…); 我收到错误: 无法将类型’System.Xml.Linq.XElement’隐式转换为’System.Xml.XmlElement’ 搜索我的服务解决方案,我看不到对System.Xml.Linq.XElement任何引用。 期待System.Xml.XmlElement或者VS 2010和我一起System.Xml.XmlElement是不是我错了?

XmlSerializer序列化空变量以使用两个标签?

我希望能够将序列化的xml类加载到Soap Envelope。 我开始所以我不填充内脏所以它看起来像: 我希望它看起来像: ` 我写的课是这样的: [System.SerializableAttribute()] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = “http://schemas.xmlsoap.org/soap/envelope/”)] [System.Xml.Serialization.XmlRootAttribute(Namespace = “http://schemas.xmlsoap.org/soap/envelope/”,ElementName=”Envelope”, IsNullable = true)] public class TestXmlEnvelope { [System.Xml.Serialization.XmlElement(ElementName=”Body”, Namespace=”http://schemas.xmlsoap.org/soap/envelope/”)] public System.Collections.ArrayList Body = new System.Collections.ArrayList(); } //class TestXmlEnvelope` 我使用它作为一个例子,因为其他人可能想要它在一个单独的元素中。 我相信这一定很简单,但遗憾的是我不知道正确的关键字。 一如既往地感谢您的帮助。 [编辑]当我尝试使用此指令时出现错误 System.Xml.Serialization.XmlSerializer xmlout = new System.Xml.Serialization.XmlSerializer(typeof(TestXmlEnvelope)); System.IO.MemoryStream memOut = new System.IO.MemoryStream(); xmlout.Serialize(memOut, envelope, namespc); Microsoft.Web.Services.SoapEnvelope soapEnv = new […]

性能优化使用生成的XmlSerializer类

我们的应用程序正在读取一些XML文件。 XML格式是固定的,因此我们可以使用XmlSerializer非常容易地读取它们。 我使用此代码读取XML文件并将其转换为类: public static T FromXml(this string xml) where T : class { if (string.IsNullOrEmpty(xml)) { return default(T); } XmlSerializer xmlserializer = new XmlSerializer(typeof(T)); XmlTextReader textReader = new XmlTextReader(new StringReader(xml)); textReader.Normalization = false; XmlReaderSettings settings = new XmlReaderSettings(); T value; using (XmlReader reader = XmlReader.Create(textReader, settings)) { value = (T)xmlserializer.Deserialize(reader); } return value; } […]