C# - YAXLib:"No elements with this name found:"
C# - YAXLib: "No elements with this name found:"
我有两个 类 用于 de/serializing 一个 XML 文件。
XmlExampleBasicUnit.cs:
[YAXSerializeAs("Unit")]
public class XmlExampleBasicUnit
{
[YAXSerializeAs("StringVar")]
public String StringVar { get; set; }
[YAXSerializeAs("Int32Var")]
public Int32 Int32Var { get; set; }
[YAXSerializeAs("DoubleVar")]
public Double DoubleVar { get; set; }
[YAXSerializeAs("DateTimeVar")]
public String DateTimeVar { get; set; }
[YAXSerializeAs("CharVar")]
public String CharVar { get; set; }
}
XmlExampleCollectionOfUnits.cs:
[YAXSerializeAs("CollectionOfUnits")]
public class XmlExampleCollectionOfUnits
{
[YAXSerializeAs("Units")]
public List<XmlExampleBasicUnit> Units { get; set; }
public XmlExampleCollectionOfUnits(List<XmlExampleBasicUnit> units)
{
Units = units;
}
public XmlExampleCollectionOfUnits()
{
}
}
我正在尝试反序列化此文件:
<?xml version="1.0" encoding="utf-8"?>
<CollectionOfUnits>
<Units>
<Unit>
<StringVar>TohleJeString</StringVar>
<Int32Var>24</Int32Var>
<DoubleVar>29.6</DoubleVar>
<DateTimeVar>17.11.1968</DateTimeVar>
<CharVar>c</CharVar>
</Unit>
<Unit>
<StringVar>TohleJeTakéString</StringVar>
<Int32Var>17</Int32Var>
<DoubleVar>5.9</DoubleVar>
<DateTimeVar>06.07.1415</DateTimeVar>
<CharVar>p</CharVar>
</Unit>
<Unit>
<StringVar>NoATohleTaké</StringVar>
<Int32Var>2</Int32Var>
<DoubleVar>78.5</DoubleVar>
<DateTimeVar>06.12.1774</DateTimeVar>
<CharVar>x</CharVar>
</Unit>
</Units>
全部由这个函数处理:
public T Import<T>(String fileName) where T : class
{
YAXSerializer serializer = new YAXSerializer(typeof(T));
return (T)serializer.Deserialize(fileName);
}
当我 运行 代码时出现以下错误:No elements with this name found: './Units'.
如您所见,已经有一个名为 Units
.
的元素
使用 File.ReadAllText(fileName)
从您的路径中读取所有 xml 内容,并将其传递给 Deserialize
方法,例如。
public static T Import<T>(String fileName) where T : class
{
string xmlData = File.ReadAllText(fileName); //Read xml content from path
YAXSerializer serializer = new YAXSerializer(typeof(T));
return (T)serializer.Deserialize(xmlData); //Pass xml content to Deserialize.
}
我有两个 类 用于 de/serializing 一个 XML 文件。
XmlExampleBasicUnit.cs:
[YAXSerializeAs("Unit")]
public class XmlExampleBasicUnit
{
[YAXSerializeAs("StringVar")]
public String StringVar { get; set; }
[YAXSerializeAs("Int32Var")]
public Int32 Int32Var { get; set; }
[YAXSerializeAs("DoubleVar")]
public Double DoubleVar { get; set; }
[YAXSerializeAs("DateTimeVar")]
public String DateTimeVar { get; set; }
[YAXSerializeAs("CharVar")]
public String CharVar { get; set; }
}
XmlExampleCollectionOfUnits.cs:
[YAXSerializeAs("CollectionOfUnits")]
public class XmlExampleCollectionOfUnits
{
[YAXSerializeAs("Units")]
public List<XmlExampleBasicUnit> Units { get; set; }
public XmlExampleCollectionOfUnits(List<XmlExampleBasicUnit> units)
{
Units = units;
}
public XmlExampleCollectionOfUnits()
{
}
}
我正在尝试反序列化此文件:
<?xml version="1.0" encoding="utf-8"?>
<CollectionOfUnits>
<Units>
<Unit>
<StringVar>TohleJeString</StringVar>
<Int32Var>24</Int32Var>
<DoubleVar>29.6</DoubleVar>
<DateTimeVar>17.11.1968</DateTimeVar>
<CharVar>c</CharVar>
</Unit>
<Unit>
<StringVar>TohleJeTakéString</StringVar>
<Int32Var>17</Int32Var>
<DoubleVar>5.9</DoubleVar>
<DateTimeVar>06.07.1415</DateTimeVar>
<CharVar>p</CharVar>
</Unit>
<Unit>
<StringVar>NoATohleTaké</StringVar>
<Int32Var>2</Int32Var>
<DoubleVar>78.5</DoubleVar>
<DateTimeVar>06.12.1774</DateTimeVar>
<CharVar>x</CharVar>
</Unit>
</Units>
全部由这个函数处理:
public T Import<T>(String fileName) where T : class
{
YAXSerializer serializer = new YAXSerializer(typeof(T));
return (T)serializer.Deserialize(fileName);
}
当我 运行 代码时出现以下错误:No elements with this name found: './Units'.
如您所见,已经有一个名为 Units
.
使用 File.ReadAllText(fileName)
从您的路径中读取所有 xml 内容,并将其传递给 Deserialize
方法,例如。
public static T Import<T>(String fileName) where T : class
{
string xmlData = File.ReadAllText(fileName); //Read xml content from path
YAXSerializer serializer = new YAXSerializer(typeof(T));
return (T)serializer.Deserialize(xmlData); //Pass xml content to Deserialize.
}