我的XML看起来像这样:
<cars year="2009">
<engineType name="blah">
<part type="metal"></part>
</engineType>
<makes>
<make name="honda" id="1">
<models>
<model name="accord" id="2"/>
</models>
</make>
</makes>
</cars>
Run Code Online (Sandbox Code Playgroud)
如何创建一个在反序列化时生成上述xml布局的类.
我有一个非常基本的类,它是一个子类列表,加上一些摘要数据.
[Serializable]
public class ProductCollection : List<Product>
{
public bool flag { get; set; }
public double A { get; set; }
public double B { get; set; }
public double C { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
...
// method to save this class
private void SaveProductCollection()
{
// Export case as XML...
XmlSerializer xml = new XmlSerializer(typeof(ProductCollection));
StreamWriter sw = new StreamWriter("output.xml");
xml.Serialize(sw, theCollection);
sw.Close();
}
Run Code Online (Sandbox Code Playgroud)
SaveProductCollection()我得到以下内容:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Product>
<InputType>1</InputType>
</Product>
<Product>
<InputType>1</InputType> …Run Code Online (Sandbox Code Playgroud) 我正在探索MSMQ服务,我编写了一个简单的控制台客户端 - 服务器应用程序,它将每个客户端的击键发送到服务器.只要打一个控制字符(DEL,ESC,INS等)服务器理解抛出一个错误.但是,每当我键入空格字符时,服务器都会收到数据包,但不会抛出错误,也不会显示空格.
服务器:
namespace QIM
{
class Program
{
const string QUEUE = @".\Private$\qim";
static MessageQueue _mq;
static readonly object _mqLock = new object();
static XmlSerializer xs;
static void Main(string[] args)
{
lock (_mqLock)
{
if (!MessageQueue.Exists(QUEUE))
_mq = MessageQueue.Create(QUEUE);
else
_mq = new MessageQueue(QUEUE);
}
xs = new XmlSerializer(typeof(string));
_mq.BeginReceive(new TimeSpan(0, 1, 0), new object(), OnReceive);
while (Console.ReadKey().Key != ConsoleKey.Escape) { }
}
static void OnReceive(IAsyncResult result)
{
Message msg;
lock (_mqLock) …Run Code Online (Sandbox Code Playgroud) 我正在开发一个系统,通过Exchange Web服务从电子邮件中获取XML附件,并通过我创建的自定义DAL对象将它们输入到数据库中.
我已经设法提取XML附件并将其作为流准备好......他们的问题是如何解析此流并填充DAL对象.
我可以创建一个XMLTextReader并遍历每个元素.我没有看到任何问题,除了我怀疑有一个更光滑的方式.读者似乎将开始标记,标记内容和结束标记视为不同的元素(使用reader.NodeType).我希望myValue被认为是一个元素而不是三个元素.就像我说的,我可以解决这个问题,但我确信必须有更好的方法.
我遇到了使用XML Serializer(对我来说是全新的)的想法,但快速查看表明这些无法处理ArrayLists和List(我正在使用List).
同样,我是LINQ的新手,但也提到了LINQ-to-XML,但我看到的例子似乎相当复杂 - 尽管我只是缺乏熟悉感.
基本上,我不想要一个cludged系统,但我不想使用任何复杂的技术与学习曲线,只是因为它是'酷'.
将此XML/Stream转换为DAL对象的最简单,最有效的方法是什么?
XML示例:
<?xml version="1.0" encoding="UTF-8"?>
<enquiry>
<enquiryno>100001</enquiryno>
<companyname>myco</companyname>
<typeofbusiness>dunno</typeofbusiness>
<companyregno>ABC123</companyregno>
<postcode>12345</postcode>
<contactemail>me@example.com</contactemail>
<firstname>My</firstname>
<lastname>Name</lastname>
<vehicles>
<vehicle>
<vehiclereg>54321</vehiclereg>
<vehicletype>Car</vehicletype>
<vehiclemake>Ford</vehiclemake>
<cabtype>n/a</cabtype>
<powerbhp>130</powerbhp>
<registrationdate>01/01/2003</registrationdate>
</vehicle>
</vehicles>
</enquiry>
Run Code Online (Sandbox Code Playgroud)
更新1:我正在尝试反序列化,基于格雷厄姆的例子.我想我已经为序列化设置了DAL,包括[XmlElement("whatever")]为每个属性指定.我尝试使用以下方法反序列化:
SalesEnquiry enquiry = null;
XmlSerializer serializer = new XmlSerializer(typeof(SalesEnquiry));
enquiry = (SalesEnquiry)serializer.Deserialize(stream);
Run Code Online (Sandbox Code Playgroud)
但是,我得到一个例外:' There is an error in XML document (2, 2)'.不良情绪表明{"<enquiry xmlns=''> was not expected."}
结论(更新):
我之前的问题是XML文件中的元素(查询)!=类的名称(SalesEnquiry).而不是一个[XmlElement]class属性,我们需要一个[XmlRoot]替代属性.为了完整性,如果希望在序列化期间忽略类中的[XmlIgnore]属性,则使用该属性.
我已成功序列化了我的对象,现在已成功获取传入的XML并将其反序列化为SalesEnquiry对象.
这种方法比手动解析XML要容易得多.好吧,学习曲线陡峭,但值得. …
我是XML序列化的新手,我已经读过private变量在公共属性下给出之前无法序列化.但是在反序列化后进行调试时,我也可以在反序列化对象中找到私有变量.有人可以解释一下吗?这是我的代码:
class Program
{
static void Main(string[] args)
{
XmlSerializer xs = new XmlSerializer(typeof(Nokia));
Nokia n = new Nokia();
using (Stream s = new FileStream("XMLFile", FileMode.Create, FileAccess.Write, FileShare.None))
{
xs.Serialize(s, n);
}
XmlSerializer xs1 = new XmlSerializer(typeof(Nokia));
using (Stream ds = File.OpenRead("XMLFile"))
{
Nokia dn = (Nokia)xs1.Deserialize(ds);
}
}
}
public class Mobile
{
public int Height = 10;
private int weight = 20;
public Mobile() {}
}
public class Nokia : Mobile
{
public string Signal = …Run Code Online (Sandbox Code Playgroud) 我有一个XML文件:
<Hand cards="C5,SQ,DQ,H8,C9,H7,S9,D5,DA,CJ,S6,HK,D4">
</Hand>
Run Code Online (Sandbox Code Playgroud)
我定义了一个类
[Serializable()]
[XmlRoot("Hand")]
public class Hand
{
[XmlAttribute("cards")]
public List<string> Cards{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下如何将XML反序列化为对象?手对象结果必须具有卡= {C5,SQ,DQ,H8,C9,H7,S9,D5,DA,CJ,S6,HK,D4}.
在Json中,我可以这样做:
[JsonProperty("type")]
[JsonConverter(typeof(MyTpeConverter))]
public BoxType myType { get; set; }
.....
public class BoxTypeEnumConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
....
}
}
Run Code Online (Sandbox Code Playgroud)
使用XML时也可能吗?
[XmlElement("isFolder")]
[XmlConvert()] // ???
public string IsFolder { get; set; }
Run Code Online (Sandbox Code Playgroud)
我的Xml文件有例如
....
<isFolder>t</isFolder>
....
Run Code Online (Sandbox Code Playgroud)
我希望“ t”为“ true”。
这段代码没有给出建议的结果,因为元素的顺序没有按照指定的顺序出现:
using System;
using System.IO;
using System.Xml.Serialization;
namespace XmlSerializeTest
{
class Program
{
static void Main(string[] args) {
SubClass test = new SubClass();
test.A = "A";
test.B = "B";
test.C = "C";
XmlSerializer SerializerObj = new XmlSerializer(typeof(SubClass));
TextWriter WriteFileStream = new StreamWriter(@"C:\test.xml");
SerializerObj.Serialize(WriteFileStream, test);
WriteFileStream.Close();
}
}
[Serializable()]
public class BaseClass {
[XmlElement(Order = 2)]
public string B { get; set; }
}
[Serializable()]
public class SubClass : BaseClass {
[XmlElement(Order = 1)]
public string A { get; set; } …Run Code Online (Sandbox Code Playgroud) 我有下面的类,我需要使用XML序列化器进行序列化
我知道XML序列化器不喜欢接口
但是,我们的编码标准通常涉及始终对接口而非特定类进行编码
我想到了一种无需更改会导致许多编译问题以及违反我们标准的类型就可以流式传输此列表的方法
我的想法是拥有一个属性,该属性可以读取“计量表”列表并将其转换为具体的类,然后在进行序列化时完全忽略计量表
但是,XML仍需要使用Meters作为元素名称
但是这不起作用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml.Serialization;
public class MeterWalkOrder
{
public MeterWalkOrder()
{
Meters = new List<IMeter>();
}
public String Name { get; set; }
[XmlIgnore]
public List<IMeter> Meters { get; set; }
[XmlArrayItem(ElementName = "Meter")]
[XmlElement(ElementName = "Meters")]
public List<Meter> SerializableMeters
{
get
{
return Meters.Cast<Meter>().ToList();
}
set
{
Meters = new List<IMeter>(value);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法解决?
我的XML(无法更改)如下
<MeterWalkOrder>
<Name>Red Route</Name>
<Meters>
<Meter>
<MeterID>1</MeterID>
<SerialNumber>12345</SerialNumber>
</Meter>
<Meter> …Run Code Online (Sandbox Code Playgroud)