反序列化XML类冲突

时间:2016-04-11 22:31:55

标签: c# .net xml serialization

我正在从XML反序列化并遇到类冲突问题。

e.g。

在我的代码中,我将引用实际居住在ContentType的{​​{1}}课程或生活在Microsoft.SharePoint.Client.ContentType的{​​{1}}课程。

e.g

Web

在我的XML中,它的内容如下:

Microsoft.SharePoint.Client.Web

在我的课程中,我正在反序列化,我有一个名为ContentType和Web的类(很确定它已被命名为this,因为我正在使用带有名称的元素对该XML进行反序列化)。

var parentCtType = (from c in rootWeb.ContentTypes
                                        where c.Name.ToLower().Trim() == ctParent.ToLower().Trim()
                                        select c).FirstOrDefault();

所以上面的这个类必须被称为ContentType(?),因为那是我从XML反序列化的。

我得到的错误就像:

<ContentTypes>
            <ContentType Name="D Base Document Set" Parent="Document Set" Group="*D">
                <ContentTypeSiteColumns>
                    <Sitecolumn DisplayName="Security" StaticName="SecurityClassification" Group="*DIAColumn" Type="TaxonomyFieldType" TermStore="D" TermSet="Security Classification" DefaultValue="UNCLASSIFIED" Required="TRUE" />
                    <Sitecolumn DisplayName="Enterprise" StaticName="Tax" Group="Enterprise Keywords Group" Type="TaxonomyFieldTypeMulti" TermStore="D" TermSet="N/A" />
                </ContentTypeSiteColumns>
            </ContentType>
            <ContentType Name="D Base Document" Parent="Document" Description="The content type from which all other document content types will inherit." Group="*D">
                <ContentTypeSiteColumns>
                    <Sitecolumn DisplayName="Security Classification" StaticName="SecurityClassification" Group="*D" Type="TaxonomyFieldType" TermStore="D" TermSet="Security" DefaultValue="UNCLASSIFIED" Required="TRUE" />
                    <Sitecolumn DisplayName="Enterprise" StaticName="Tax" Group="Enterprise" Type="TaxonomyFieldTypeMulti" TermStore="D" TermSet="N/A" />
                    <Sitecolumn DisplayName="Notes" StaticName="Notess" Description="For notes giving context to the document.  Additional information can include URL link to another document." Group="*D" Type="Note" Required="FALSE" />
                </ContentTypeSiteColumns>
            </ContentType>

如果我将该类称为反序列化,我会消失,例如public class ContentType { [XmlAttribute] public string Name; [XmlAttribute] public string Parent; public List<Sitecolumn> ContentTypeSiteColumns; } ,但反序列化过程不起作用。

我该如何解决这个问题?希望它有意义。

1 个答案:

答案 0 :(得分:0)

如果您希望反序列化XML,那么我就是这样做的....

Usings .....

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

类.....

[XmlRoot(ElementName = "Sitecolumn")]
public class Sitecolumn
{
    [XmlAttribute(AttributeName = "DisplayName")]
    public string DisplayName { get; set; }
    [XmlAttribute(AttributeName = "StaticName")]
    public string StaticName { get; set; }
    [XmlAttribute(AttributeName = "Group")]
    public string Group { get; set; }
    [XmlAttribute(AttributeName = "Type")]
    public string Type { get; set; }
    [XmlAttribute(AttributeName = "TermStore")]
    public string TermStore { get; set; }
    [XmlAttribute(AttributeName = "TermSet")]
    public string TermSet { get; set; }
    [XmlAttribute(AttributeName = "DefaultValue")]
    public string DefaultValue { get; set; }
    [XmlAttribute(AttributeName = "Required")]
    public string Required { get; set; }
    [XmlAttribute(AttributeName = "Description")]
    public string Description { get; set; }
}

[XmlRoot(ElementName = "ContentTypeSiteColumns")]
public class ContentTypeSiteColumns
{
    [XmlElement(ElementName = "Sitecolumn")]
    public List<Sitecolumn> Sitecolumn { get; set; }
}

[XmlRoot(ElementName = "ContentType")]
public class ContentType
{
    [XmlElement(ElementName = "ContentTypeSiteColumns")]
    public ContentTypeSiteColumns ContentTypeSiteColumns { get; set; }
    [XmlAttribute(AttributeName = "Name")]
    public string Name { get; set; }
    [XmlAttribute(AttributeName = "Parent")]
    public string Parent { get; set; }
    [XmlAttribute(AttributeName = "Group")]
    public string Group { get; set; }
    [XmlAttribute(AttributeName = "Description")]
    public string Description { get; set; }
}

[XmlRoot(ElementName = "ContentTypes")]
public class ContentTypes
{
    [XmlElement(ElementName = "ContentType")]
    public List<ContentType> ContentType { get; set; }
}

代码.....

class Program
{
    static void Main(string[] args)
    {
        string strXML = File.ReadAllText("xml.xml");
        byte[] bufXML = ASCIIEncoding.UTF8.GetBytes(strXML);
        MemoryStream ms1 = new MemoryStream(bufXML);

        // Deserialize to object
        XmlSerializer serializer = new XmlSerializer(typeof(ContentTypes));
        try
        {
            using (XmlReader reader = new XmlTextReader(ms1))
            {
                ContentTypes deserializedXML = (ContentTypes)serializer.Deserialize(reader);

            }// put a break point here and mouse-over deserializedXML….
        }
        catch (Exception ex)
        {
            throw;
        }

    }
}

更新1

这是XML

<ContentTypes>
            <ContentType Name="D Base Document Set" Parent="Document Set" Group="*D">
                <ContentTypeSiteColumns>
                    <Sitecolumn DisplayName="Security" StaticName="SecurityClassification" Group="*DIAColumn" Type="TaxonomyFieldType" TermStore="D" TermSet="Security Classification" DefaultValue="UNCLASSIFIED" Required="TRUE" />
                    <Sitecolumn DisplayName="Enterprise" StaticName="Tax" Group="Enterprise Keywords Group" Type="TaxonomyFieldTypeMulti" TermStore="D" TermSet="N/A" />
                </ContentTypeSiteColumns>
            </ContentType>
            <ContentType Name="D Base Document" Parent="Document" Description="The content type from which all other document content types will inherit." Group="*D">
                <ContentTypeSiteColumns>
                    <Sitecolumn DisplayName="Security Classification" StaticName="SecurityClassification" Group="*D" Type="TaxonomyFieldType" TermStore="D" TermSet="Security" DefaultValue="UNCLASSIFIED" Required="TRUE" />
                    <Sitecolumn DisplayName="Enterprise" StaticName="Tax" Group="Enterprise" Type="TaxonomyFieldTypeMulti" TermStore="D" TermSet="N/A" />
                    <Sitecolumn DisplayName="Notes" StaticName="Notess" Description="For notes giving context to the document.  Additional information can include URL link to another document." Group="*D" Type="Note" Required="FALSE" />
                </ContentTypeSiteColumns>
            </ContentType>
</ContentTypes>

我正在从名为xml.xml的应用程序构建文件夹中的文件中读取XML到一个字符串...您需要从其他地方获取XML字符串或创建xml.xml文件并保存XML上面的代码工作