C# C语言中复杂XML的反序列化#
我有一个compex XML,如下所示:C# C语言中复杂XML的反序列化#,c#,xml,deserialization,C#,Xml,Deserialization,我有一个compex XML,如下所示: <rootentity name='xyz'> <cascadingentities> <cascadingentity name='xyz'> <fetchXML> <fetch> <entity name='xyz' > <attribute name='
<rootentity name='xyz'>
<cascadingentities>
<cascadingentity name='xyz'>
<fetchXML>
<fetch>
<entity name='xyz' >
<attribute name='xyz' />
<filter type='and' >
<condition attribute='xyz' operator='eq' value='{xyz}' />
</filter>
</entity>
</fetch>
</fetchXML>
<cascadingentities>
<cascadingentity name='xyz'>
<fetchXML>
<fetch>
<entity name='xyz' >
<attribute name='xyz' />
<filter type='and' >
<condition attribute='xyz' operator='eq' value='{xyz}' />
</filter>
</entity>
</fetch>
</fetchXML>
<cascadingentities>
<cascadingentity name='xyz'>
<fetchXML>
<fetch>
<entity name='xyz' >
<attribute name='xyz' />
<filter type='and' >
<condition attribute='xyz' operator='eq' value='{xyz}' />
</filter>
</entity>
</fetch>
</fetchXML>
</cascadingentity>
</cascadingentities>
</cascadingentity>
</cascadingentities>
</cascadingentity>
<cascadingentity name='xyz'>
<fetchXML>
<fetch>
<entity name='xyz' >
<attribute name='xyz' />
<filter type='and' >
<condition attribute='xyz' operator='eq' value='{xyz}' />
</filter>
</entity>
</fetch>
</fetchXML>
</cascadingentity>
</cascadingentities>
注意,元素“cascadingentity”可以嵌套在“cascadingentity”中
有人能告诉我如何实现这一目标的正确方向吗?也许需要一些代码示例
非常感谢您的帮助
亲切问候这对我很有效,我需要在xml中添加结束标记,尽管是“”:
void Main()
{
var rootelement=new XmlSerializer(typeof(Rootentity));
}
//在此处定义其他方法和类
[XmlRoot(ElementName=“rootentity”)]
公共类根实体
{
[xmlement(ElementName=“cascadingentities”)]
公共cascadingenties cascadingenties{get;set;}
[XmlAttribute(AttributeName=“name”)]
公共字符串名称{get;set;}
}
[XmlRoot(ElementName=“cascadingentities”)]
公营绅士
{
[xmlement(ElementName=“cascadingentity”)]
公共Cascadingentity Cascadingentity{get;set;}
}
[XmlRoot(ElementName=“cascadingentity”)]
公共阶层的绅士风度
{
[xmlement(ElementName=“fetchXML”)]
公共FetchXML FetchXML{get;set;}
[XmlAttribute(AttributeName=“name”)]
公共字符串名称{get;set;}
}
[XmlRoot(ElementName=“fetchXML”)]
公共类获取XML
{
[xmlement(ElementName=“fetch”)]
公共获取{get;set;}
}
[XmlRoot(ElementName=“fetch”)]
公共类获取
{
[xmlement(ElementName=“entity”)]
公共实体{get;set;}
}
[XmlRoot(ElementName=“entity”)]
公共类实体
{
[xmlement(ElementName=“attribute”)]
公共属性{get;set;}
[xmlement(ElementName=“filter”)]
公共筛选器筛选器{get;set;}
[XmlAttribute(AttributeName=“name”)]
公共字符串名称{get;set;}
}
[XmlRoot(ElementName=“attribute”)]
公共类属性
{
[XmlAttribute(AttributeName=“name”)]
公共字符串名称{get;set;}
}
[XmlRoot(ElementName=“filter”)]
公共类过滤器
{
[XmlElement(ElementName=“条件”)]
公共条件条件{get;set;}
[XmlAttribute(AttributeName=“type”)]
公共字符串类型{get;set;}
}
[XmlRoot(ElementName=“条件”)]
公共阶级状况
{
[XmlAttribute(AttributeName=“attribute”)]
公共字符串属性{get;set;}
[XmlAttribute(AttributeName=“operator”)]
公共字符串运算符{get;set;}
[XmlAttribute(AttributeName=“value”)]
公共字符串值{get;set;}
}
字符串xmlText=@”
";
试试看
[XmlRoot(ElementName=“rootentity”)]
公共类根实体
{
[xmlement(ElementName=“cascadingentities”)]
公共列表Cascadingentities{get;set;}
[XmlAttribute(AttributeName=“name”)]
公共字符串名称{get;set;}
}
对于“集合”,这允许多个嵌入式
Cascadingentity
元素。
XML序列化程序支持列表
您将不再需要
Cascadingentities
类型,然后…您可以使用xsd.exe自动生成类cascadingentities
应该是一个列表
属性。这样就可以序列化该集合。到底是什么在这里不起作用?除了缺少的列表,它看起来还不错。谢谢回复。我正在努力解决的是如何实现它,特别是递归部分。谢谢你的提示。这到底应该是什么?当我使用xsd.exe时,我得到了一个错误:同一个表'cascadingentities'不能是两个嵌套关系中的子表。“嗨,汤姆,非常感谢你的反馈。但是正如您所看到的,有一个递归,因此元素“cascadingentities”嵌套在子元素中,没有任何限制。它可以嵌套多次。
[XmlRoot(ElementName = "rootentity")]
public class Rootentity
{
[XmlElement(ElementName = "cascadingentities")]
public Cascadingentities Cascadingentities { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "cascadingentities")]
public class Cascadingentities
{
[XmlElement(ElementName = "cascadingentity")]
public Cascadingentity Cascadingentity { get; set; }
}
[XmlRoot(ElementName = "cascadingentity")]
public class Cascadingentity
{
[XmlElement(ElementName = "fetchXML")]
public FetchXML FetchXML { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "fetchXML")]
public class FetchXML
{
[XmlElement(ElementName = "fetch")]
public Fetch Fetch { get; set; }
}
[XmlRoot(ElementName = "fetch")]
public class Fetch
{
[XmlElement(ElementName = "entity")]
public Entity Entity { get; set; }
}
[XmlRoot(ElementName = "entity")]
public class Entity
{
[XmlElement(ElementName = "attribute")]
public Attribute Attribute { get; set; }
[XmlElement(ElementName = "filter")]
public Filter Filter { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "attribute")]
public class Attribute
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "filter")]
public class Filter
{
[XmlElement(ElementName = "condition")]
public Condition Condition { get; set; }
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
}
[XmlRoot(ElementName = "condition")]
public class Condition
{
[XmlAttribute(AttributeName = "attribute")]
public string Attribute { get; set; }
[XmlAttribute(AttributeName = "operator")]
public string Operator { get; set; }
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
}
void Main()
{
var rootelement = new XmlSerializer(typeof(Rootentity)).Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(xmlText)));
}
// Define other methods and classes here
[XmlRoot(ElementName = "rootentity")]
public class Rootentity
{
[XmlElement(ElementName = "cascadingentities")]
public Cascadingentities Cascadingentities { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "cascadingentities")]
public class Cascadingentities
{
[XmlElement(ElementName = "cascadingentity")]
public Cascadingentity Cascadingentity { get; set; }
}
[XmlRoot(ElementName = "cascadingentity")]
public class Cascadingentity
{
[XmlElement(ElementName = "fetchXML")]
public FetchXML FetchXML { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "fetchXML")]
public class FetchXML
{
[XmlElement(ElementName = "fetch")]
public Fetch Fetch { get; set; }
}
[XmlRoot(ElementName = "fetch")]
public class Fetch
{
[XmlElement(ElementName = "entity")]
public Entity Entity { get; set; }
}
[XmlRoot(ElementName = "entity")]
public class Entity
{
[XmlElement(ElementName = "attribute")]
public Attribute Attribute { get; set; }
[XmlElement(ElementName = "filter")]
public Filter Filter { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "attribute")]
public class Attribute
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "filter")]
public class Filter
{
[XmlElement(ElementName = "condition")]
public Condition Condition { get; set; }
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
}
[XmlRoot(ElementName = "condition")]
public class Condition
{
[XmlAttribute(AttributeName = "attribute")]
public string Attribute { get; set; }
[XmlAttribute(AttributeName = "operator")]
public string Operator { get; set; }
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
}
string xmlText = @"<rootentity name='xyz'>
<cascadingentities>
<cascadingentity name='xyz'>
<fetchXML>
<fetch>
<entity name='xyz' >
<attribute name='xyz' />
<filter type='and' >
<condition attribute='xyz' operator='eq' value='{xyz}' />
</filter>
</entity>
</fetch>
</fetchXML>
<cascadingentities>
<cascadingentity name='xyz'>
<fetchXML>
<fetch>
<entity name='xyz' >
<attribute name='xyz' />
<filter type='and' >
<condition attribute='xyz' operator='eq' value='{xyz}' />
</filter>
</entity>
</fetch>
</fetchXML>
<cascadingentities>
<cascadingentity name='xyz'>
<fetchXML>
<fetch>
<entity name='xyz' >
<attribute name='xyz' />
<filter type='and' >
<condition attribute='xyz' operator='eq' value='{xyz}' />
</filter>
</entity>
</fetch>
</fetchXML>
</cascadingentity>
</cascadingentities>
</cascadingentity>
</cascadingentities>
</cascadingentity>
<cascadingentity name='xyz'>
<fetchXML>
<fetch>
<entity name='xyz' >
<attribute name='xyz' />
<filter type='and' >
<condition attribute='xyz' operator='eq' value='{xyz}' />
</filter>
</entity>
</fetch>
</fetchXML>
</cascadingentity>
</cascadingentities>
</rootentity>";