我有以下C#类
[XmlTypeAttribute(AnonymousType = true)]
public class TransactionTypesHolder
{
[XmlArrayItem("TransactionType", typeof(TransactionType))]
public List<TransactionType> TransactionTypes { get; set; }
public TransactionTypesHolder()
{
TransactionTypes = new List<TransactionType>();
}
}
public class TransactionType
{
[XmlElement("Mti")]
public string Mti { get; set; }
[XmlElement("Description")]
public string Description { get; set; }
//[XmlArrayItem("Field", typeof(Field))]
//public List<Field> Fields { get; set; }
[XmlArray(ElementName = "Fields")]
[XmlArrayItem("Field", typeof(Field))]
public List<Field> Fields { get; set; }
}
[Serializable]
public class Field
{
[XmlElement("FieldNumber")]
public string FieldNumber { get; set; }
[XmlElement("Description")]
public string Description { get; set; }
//[XmlArrayItem("FieldPart", typeof(FieldPart))]
//public List<FieldPart> FieldParts { get; set; }
[XmlArray(ElementName = "FieldParts")]
[XmlArrayItem("FieldPart", typeof(FieldPart))]
public List<FieldPart> FieldParts { get; set; }
}
[Serializable]
public class FieldPart
{
[XmlElement("PartNumber")]
public string PartNumber { get; set; }
[XmlElement("PartValue")]
public string PartValue { get; set; }
}
我试图通过以下代码反序列化
try
{
XmlSerializer serializer = new
XmlSerializer(typeof(TransactionTypesHolder));
// A FileStream is needed to read the XML document.
FileStream fs = new FileStream("Xmlfile1.xml", FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
// Declare an object variable of the type to be deserialized.
TransactionTypesHolder i;
// Use the Deserialize method to restore the object's state.
i = (TransactionTypesHolder)serializer.Deserialize(reader);
fs.Close();
}
catch (Exception ex)
{
}
但预计不会出现错误。
我的xml是
<?xml version="1.0" ?>
<TransactionTypes>
<TransactionType>
<Mti>200</Mti>
<Description>Debit</Description>
<Fields>
<Field>
<FieldNumber>3</FieldNumber>
<Description></Description>
<FieldParts>
<FieldPart>
<PartNumber>
1
</PartNumber>
<PartValue>
00
</PartValue>
<Description></Description>
</FieldPart>
<FieldPart>
<PartNumber>
2
</PartNumber>
<PartValue>
00
</PartValue>
<Description></Description>
</FieldPart>
</FieldParts>
</Field>
<Field>
<FieldNumber>14</FieldNumber>
<Description></Description>
<FieldParts>
<FieldPart>
<PartNumber>
1
</PartNumber>
<PartValue>
9911
</PartValue>
<Description></Description>
</FieldPart>
</FieldParts>
</Field>
</Fields>
</TransactionType>
<TransactionType>
<Mti>100</Mti>
<Description>Balance</Description>
<Fields>
<Field>
<FieldNumber>3</FieldNumber>
<Description></Description>
<FieldParts>
<FieldPart>
<PartNumber>
1
</PartNumber>
<PartValue>
31
</PartValue>
<Description></Description>
</FieldPart>
<FieldPart>
<PartNumber>
2
</PartNumber>
<PartValue>
00
</PartValue>
<Description></Description>
</FieldPart>
</Field>
<Field>
<FieldNumber>14</FieldNumber>
<Description></Description>
<FieldParts>
<FieldPart>
<PartNumber>
1
</PartNumber>
<PartValue>
9911
</PartValue>
<Description></Description>
</FieldPart>
</FieldParts>
</Field>
</Fields>
</TransactionType>
</TransactionTypes>
答案 0 :(得分:0)
这看起来不对:
$('a').each(function () {
var href = $(this).prop('href');
$(this).click(function (e) {
e.preventDefault();
myFunc(href);
});
$(this).removeProp('href');
});
尝试更改为:
[XmlElement("TransactionTypes")]
public List<TransactionType> TransactionTypes { get; set; }
同样改变:
[XmlArray(ElementName="TransactionTypes")]
[XmlArrayItem("TransactionType", typeof(TransactionType))]
public List<TransactionType> TransactionTypes { get; set; }
为:
[XmlArrayItem("Field", typeof(Field))]
public List<Field> Fields { get; set; }
和以下相同:
[XmlArray(ElementName="Fields")]
[XmlArrayItem("Field", typeof(Field))]
public List<Field> Fields { get; set; }
为:
[XmlArrayItem("FieldPart", typeof(FieldPart))]
public List<FieldPart> FieldParts { get; set; }
答案 1 :(得分:0)
我使用了以下课程
[XmlRoot("TransactionTypes")]
public class TransactionTypes
{
[XmlElement("TransactionType")]
public List<TransactionType> TransactionType { get; set; }
}
而不是TransactionTypesHolder并成功地反序列化xml