如何为上述XML标记创建模型类? [英] How to create model class for the mentioned XML tag ?
问题描述
如何为xml标记创建模型类。在反序列化获取异常时,模型类的reflcetion类型中存在错误。
我的Xml如下:
How to create model class for the xml tag. On deserializing getting the exception there is an error in reflcetion type of Model class.
My Xml is as below:
<advice_tags>
<aclassname>SAV1 ACCOUNT</aclassname>
<blockedbalance>0</blockedbalance>
<acy>
<blocked_acy>23</blocked_acy>
<amount_acy>56</amount_acy>
</acy>
</advice_tags>
我上面的Xml标签的Model类如下:
My Model class for the above Xml Tag is as below:
[XmlElement("ACLASSNAME")]
public string ACLASSNAME { get; set; }
[XmlElement("BLOCKEDBALANCE")]
public string BLOCKEDBALANCE { get; set; }
[XmlArray("ACY")]
[XmlArrayItem("BLOCKED_ACY")]
[XmlArrayItem("AMOUNT_ACY")]
public List<string> ACY { get; set; }
但是我对XmlArray属性进行脱盐处理时遇到异常。
But I am getting exception on desalinizing the same for the XmlArray attribute.
MDL_UBSFile UBSFile = null;
XmlSerializer serializer = new XmlSerializer(typeof(MDL_UBSFile));
reader = new StreamReader(Path.Combine(UBSFilePath, FileName));
dss.ReadXml(reader);
reader.Close();
推荐答案
你的xml对大小写字母都很敏感,所以说
Your xml become sensitive to big and small letters, so say
<myclass>
<myproperty>1</myproperty>
</myclass>
不是
is not
public class MyClass{
public int MyProperty{ get;set;}
}
宁愿是
rather would be
public class myclass{
public int myproperty{ get;set;}
}
所以你需要做的就是匹配你的写作方式
......(更新解决方案)......
所以如果这不起作用,我建议走另一条路。让课程首先
通过观察你的xml很清楚,可以使用两个简单类型和一个名为Acy的复杂类型生成一个类AdviceTag,其中包含两个值(Blocked)和金额),或许是这样的:
so what you need to do is match the way you wrote it
... (updating solution) ...
So if that doesn't work, i suggest going the other way about. Make the class first
and it is clear by observing your xml that a class AdviceTag could be generated with two simply types and one complex type called Acy with two values (Blocked and amount), perhaps like this:
public class AdviceTag
{
public string AClassName { get; set; }
public string BlockedBalance { get; set; }
public Acy Acy { get; set; }
}
public class Acy
{
public string Blocked{ get; set; }
public string Amount { get; set; }
}
这样就可以通过实例化该类的实例并将其序列化来生成xml。 />
so that in place let's simply generate the xml by instantiating an instance of that class and serialize it
var aTag = new AdviceTag { AClassName = "Sav1 Account", BlockedBalance = "0", Acy = new Acy { Amount = "56", Blocked = "23" } };
var ser = new XmlSerializer(typeof(AdviceTag));
string filename = Assembly.GetExecutingAssembly().Location;
int pos = filename.LastIndexOf(@"\");
filename = filename.Substring(0, pos +1) + "myfile.xml";
using(var fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
{
ser.Serialize(fs, aTag);
fs.Flush();
fs.Close();
}
我们将序列化的xml放在与执行程序集相同的位置,并将其命名为myfile.xml,然后看起来像这样:
We're placing the serialized xml in same location as executing assembly and calling it myfile.xml, it then looks like this:
<?xml version="1.0"?>
<AdviceTag xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AClassName>Sav1 Account</AClassName>
<BlockedBalance>0</BlockedBalance>
<Acy>
<Blocked>23</Blocked>
<Amount>56</Amount>
</Acy>
</AdviceTag>
现在出现了什么错误?
那么你的xml指的是一个复杂的类型名为acy,包含两个成员属性,但你很难将它们放在一个arraylist中,你不必这样做。
And now what was the mistake?
Well your xml designates a complex type called acy containing two member properties, but you were struggleing to put them in an arraylist, where you don't have to.
这篇关于如何为上述XML标记创建模型类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!