C# XML反序列化在C中提供结构而不是数据#
我正在尝试反序列化xml文件。正在获取结构,但没有数据。请帮忙 以下是我正在使用的文件/类: 1.XML文件: c。模块类别:C# XML反序列化在C中提供结构而不是数据#,c#,xml,xml-serialization,C#,Xml,Xml Serialization,我正在尝试反序列化xml文件。正在获取结构,但没有数据。请帮忙 以下是我正在使用的文件/类: 1.XML文件: c。模块类别: [XmlRoot(ElementName = "Module")] public class Module { [XmlAttribute(AttributeName = "Module")] public string Modul { get; set; } [XmlArrayItem("Parameter")] public Lis
[XmlRoot(ElementName = "Module")]
public class Module
{
[XmlAttribute(AttributeName = "Module")]
public string Modul { get; set; }
[XmlArrayItem("Parameter")]
public List<ModuleParameter> Parameters { get; set; }
}
f。块类:
[XmlRoot(ElementName = "Block")]
public class Block
{
[XmlAttribute(AttributeName = "ID")]
public string ID { get; set; }
[XmlAttribute(AttributeName = "Module")]
public string Module { get; set; }
[XmlArrayItem("Parameter")]
public List<BlockParameter> Parameters { get; set; }
}
我正在使用以下代码反序列化xml文件:
var serializer = new XmlSerializer(typeof(Model));
using (FileStream fs = File.OpenRead(file))
{
var xmlClass = (Model)serializer.Deserialize(fs);
}
附言:
1.请注意,我必须使用两个参数类:一个在模块中,另一个在块类中。
2.我已将XmlElement修改为XmlAttribute,用于建议的属性,如内容、单位类别、单位等。一个更正,属性不是元素。
您可以找到其他事件
[XmlRoot("Model")]
public class Model
{
//[XmlElement(ElementName = "Content")]
[XmlAttribute(AttributeName = "Content")]
public string Content { get; set; }
您还可以添加一个空的C#文件,然后选择编辑|粘贴特殊| XML作为类。这不是最漂亮的代码,但至少它是有效的。用于比较 要获取标记的属性值,请将“XML元素”更改为
“XML属性”
例如:
到
需要的其他字段。更改包括标签、名称、类型等
如果要通过序列化创建XMl,请阅读以下内容:
[XmlRoot("Model")]
public class Model
{
[XmlAttribute(AttributeName = "Content")]
public string Content { get; set; }
[XmlArray("Units")]
[XmlArrayItem("Unit")]
public List<Unit> Units { get; set; }
[XmlArray("Modules")]
[XmlArrayItem("Module")]
public List<Module> Modules { get; set; }
[XmlArray("Blocks")]
[XmlArrayItem("Block")]
public List<Block> Blocks { get; set; }
}
[XmlRoot(ElementName = "Unit")]
public class Unit
{
[XmlAttribute(AttributeName = "UnitCategory")]
public string UnitCategory { get; set; }
[XmlAttribute(AttributeName = "Units")]
public string Units { get; set; }
}
[XmlRoot(ElementName = "Module")]
public class Module
{
[XmlAttribute(AttributeName = "Module")]
public string Modul { get; set; }
[XmlElement("Parameter")]
public List<ModuleParameter> Parameters { get; set; }
}
[XmlRoot(ElementName = "Parameter")]
public class ModuleParameter
{
[XmlAttribute(AttributeName = "Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "Type")]
public string Type { get; set; }
[XmlElement("Enumeration")]
public List<Enumeration> Enumerations { get; set; }
}
[XmlRoot(ElementName = "Enumeration")]
public class Enumeration
{
[XmlAttribute(AttributeName = "Tag")]
public string Tag { get; set; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "Block")]
public class Block
{
[XmlAttribute(AttributeName = "ID")]
public string ID { get; set; }
[XmlAttribute(AttributeName = "Module")]
public string Module { get; set; }
[XmlElement("Parameter")]
public List<BlockParameter> Parameters { get; set; }
}
[XmlRoot(ElementName = "Parameter")]
public class BlockParameter
{
[XmlAttribute(AttributeName = "Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
}
[XmlRoot(“模型”)]
公共类模型
{
[XmlAttribute(AttributeName=“Content”)]
公共字符串内容{get;set;}
[XmlArray(“单位”)]
[XmlArrayItem(“单位”)]
公共列表单元{get;set;}
[XmlArray(“模块”)]
[XmlArrayItem(“模块”)]
公共列表模块{get;set;}
[XmlArray(“块”)]
[XmlArrayItem(“块”)]
公共列表块{get;set;}
}
[XmlRoot(ElementName=“Unit”)]
公营课组
{
[XmlAttribute(AttributeName=“UnitCategory”)]
公共字符串UnitCategory{get;set;}
[XmlAttribute(AttributeName=“Units”)]
公共字符串单位{get;set;}
}
[XmlRoot(ElementName=“Module”)]
公共类模块
{
[XmlAttribute(AttributeName=“模块”)]
公共字符串模块{get;set;}
[XmlElement(“参数”)]
公共列表参数{get;set;}
}
[XmlRoot(ElementName=“Parameter”)]
公共类模块参数
{
[XmlAttribute(AttributeName=“Name”)]
公共字符串名称{get;set;}
[XmlAttribute(AttributeName=“Type”)]
公共字符串类型{get;set;}
[XmlElement(“枚举”)]
公共列表枚举{get;set;}
}
[XmlRoot(ElementName=“枚举”)]
公共类枚举
{
[XmlAttribute(AttributeName=“Tag”)]
公共字符串标记{get;set;}
[XmlAttribute(AttributeName=“Value”)]
公共字符串值{get;set;}
}
[XmlRoot(ElementName=“Block”)]
公共类街区
{
[XmlAttribute(AttributeName=“ID”)]
公共字符串ID{get;set;}
[XmlAttribute(AttributeName=“模块”)]
公共字符串模块{get;set;}
[XmlElement(“参数”)]
公共列表参数{get;set;}
}
[XmlRoot(ElementName=“Parameter”)]
公共类块参数
{
[XmlAttribute(AttributeName=“Name”)]
公共字符串名称{get;set;}
[XmlAttribute(AttributeName=“Value”)]
公共字符串值{get;set;}
}
说明:。举个例子:
有一个容器
,而
没有容器。它直接添加到父元素中
注意:如@bommelding所述,您可以删除所有
[XmlRoot(…)]
属性。不需要它们。唯一需要的是模型上的类,但该类已经具有根元素的名称。调试的最佳方法是用示例数据填充类,然后序列化。然后将序列化数据与xml进行比较。第一个明显的问题是内容是一个属性而不是一个元素。这个xml是被序列化的还是仅仅是一个示例1?看看这个,谢谢@jdweng,我已经纠正了这一部分。但尚未获得参数列表中的数据。@bommlding:谢谢,我已按属性更新了元素。现在我在反序列化对象中获取属性值。。但是参数类还没有得到任何东西。不知道粘贴特别感谢!:-)@WpfBee-我不确定,但您的类有多个问题。。。例如,应该只有一个XmlRoot。使用“粘贴”特殊技巧替换代码或与之进行彻底比较。@bommelding-我认为这是正确的。我只将模型类标记为根元素,其余部分标记为根元素。我之前也使用过它,因为它工作得很好。谢谢,我已经按属性更新了元素。现在我在反序列化对象中获取属性值。。但是参数类还没有得到任何结果。@wpfbee这个答案应该被接受,完美的回答:你为什么保留所有的XmlRoots?这有什么用吗?@bommelding没有。。。但是OP把它们放进去了,所以我把它们放进去了。。。也许他还有其他Xml,它们只是主Xml的子部分。它们都可以删除,甚至是模型上的一个,因为类与根元素具有相同的名称。@xanatos-非常感谢。这对我来说真的很有效。我现在正在获取参数列表中的项目。@xanatos-非常感谢。这对我来说真的很有效。我现在正在获取参数列表中的项目。2件事:a。发布的XML是原始文件:)b。ModuleParameter和BlockParameter类都是具有一些公共属性的参数类。我应该为模块和块使用单个参数类吗?若我使用一个类,那个么ModelParameter类将获得具有空值的附加属性(这不是必需的)。如果我需要使用两个不同的类,那么请建议如何做到这一点?
[XmlRoot(ElementName = "Enumeration")]
public class Enumeration
{
[XmlAttribute(AttributeName = "Tag")]
public string Tag { get; set; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "Block")]
public class Block
{
[XmlAttribute(AttributeName = "ID")]
public string ID { get; set; }
[XmlAttribute(AttributeName = "Module")]
public string Module { get; set; }
[XmlArrayItem("Parameter")]
public List<BlockParameter> Parameters { get; set; }
}
[XmlRoot(ElementName = "Parameter")]
public class BlockParameter
{
[XmlAttribute(AttributeName = "Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
}
var serializer = new XmlSerializer(typeof(Model));
using (FileStream fs = File.OpenRead(file))
{
var xmlClass = (Model)serializer.Deserialize(fs);
}
[XmlRoot("Model")]
public class Model
{
//[XmlElement(ElementName = "Content")]
[XmlAttribute(AttributeName = "Content")]
public string Content { get; set; }
[XmlElement(ElementName = "UnitCategory")]
public string UnitCategory { get; set; }
[XmlAttribute(AttributeName = "UnitCategory")]
public string UnitCategory { get; set; }
[XmlRoot("Model")]
public class Model
{
[XmlAttribute(AttributeName = "Content")]
public string Content { get; set; }
[XmlArray("Units")]
[XmlArrayItem("Unit")]
public List<Unit> Units { get; set; }
[XmlArray("Modules")]
[XmlArrayItem("Module")]
public List<Module> Modules { get; set; }
[XmlArray("Blocks")]
[XmlArrayItem("Block")]
public List<Block> Blocks { get; set; }
}
[XmlRoot(ElementName = "Unit")]
public class Unit
{
[XmlAttribute(AttributeName = "UnitCategory")]
public string UnitCategory { get; set; }
[XmlAttribute(AttributeName = "Units")]
public string Units { get; set; }
}
[XmlRoot(ElementName = "Module")]
public class Module
{
[XmlAttribute(AttributeName = "Module")]
public string Modul { get; set; }
[XmlElement("Parameter")]
public List<ModuleParameter> Parameters { get; set; }
}
[XmlRoot(ElementName = "Parameter")]
public class ModuleParameter
{
[XmlAttribute(AttributeName = "Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "Type")]
public string Type { get; set; }
[XmlElement("Enumeration")]
public List<Enumeration> Enumerations { get; set; }
}
[XmlRoot(ElementName = "Enumeration")]
public class Enumeration
{
[XmlAttribute(AttributeName = "Tag")]
public string Tag { get; set; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "Block")]
public class Block
{
[XmlAttribute(AttributeName = "ID")]
public string ID { get; set; }
[XmlAttribute(AttributeName = "Module")]
public string Module { get; set; }
[XmlElement("Parameter")]
public List<BlockParameter> Parameters { get; set; }
}
[XmlRoot(ElementName = "Parameter")]
public class BlockParameter
{
[XmlAttribute(AttributeName = "Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
}