【问题标题】:Deserializing XML class conflict反序列化 XML 类冲突
【发布时间】:2016-04-11 22:31:55
【问题描述】:

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

例如

在我的代码中,我将引用实际存在于Microsoft.SharePoint.Client.ContentType 中的ContentType 类或存在于Microsoft.SharePoint.Client.Web 中的Web 类。

例如

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

在我的 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>

在我要反序列化的类中,我有一个名为 ContentType 和 Web 的类(很确定它已命名为 this,因为我使用该名称的元素从该 XML 反序列化)。

public class ContentType
    {
        [XmlAttribute]
        public string Name;

        [XmlAttribute]
        public string Parent;

        public List<Sitecolumn> ContentTypeSiteColumns;

    }

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

我得到的错误是这样的:

Error   4   'testebby.ContentType' does not contain a definition for 'FieldLinks' and no extension method 'FieldLinks' accepting a first argument of type 'testebby.ContentType' could be found (are you missing a using directive or an assembly reference?)

如果我将要反序列化的类调用为不同的东西,例如 ContentTypeZ,但反序列化过程不起作用。

我该如何解决这个问题?希望有道理。

【问题讨论】:

    标签: c# .net xml serialization


    【解决方案1】:

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

    用途.....

    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 保存为上面的代码可以工作

    【讨论】:

    • 我仍然遇到同样的问题。这里还有一个类叫做 ContentType
    • 您的 XML(如您发布的那样)不完整,缺少结束标记 ,我不知道这是否是故意的......我已经更新了我的答案包含 标记的 XML 并且一切正常,整个 XML 被反序列化为 deserializedXML
    • 是的,本来应该在那里,但我发送的内容不完整。我仍然遇到此类 ContentType 和 Microsoft.Sharepoint.Client.ContentType 之间的类冲突
    • 那么我需要查看更多代码和所有 XML,我的答案中的代码将在您发布时对 XML(加上我添加的结束标记)进行反序列化......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多