在C#中对复杂XML进行去激活

时间:2017-10-09 10:58:05

标签: c# xml deserialization

我有一个类似以下的compex XML:

<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>

我需要将整个事件反序列化为c#类。请注意,这必须以递归方式完成,因为某些元素是嵌套的。

我的班级定义如下:

[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; }
}

Nesting

注意元素&#34; cascadingentities&#34;可以嵌套在&#34; cascadingentity&#34;

可以请任何人指出我如何实现这一目标的正确方向?也许有一些代码样本?

非常感谢任何帮助。

亲切的问候

2 个答案:

答案 0 :(得分:1)

这对我有用,我需要在xml中添加结束标记#34;&#34;:

    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>";

答案 1 :(得分:0)

[XmlRoot(ElementName = "rootentity")] public class Rootentity { [XmlElement(ElementName = "cascadingentities")] public List<CascadingEntity> Cascadingentities { get; set; } [XmlAttribute(AttributeName = "name")] public string Name { get; set; } }

用于“集合”这允许多个嵌入式     Cascadingentity 元素。 XML序列化程序支持列表。

你根本不需要Cascadingentities类型......