亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何解決:“您需要將XmlChoiceIdentifierAttribute添加到成員中”

如何解決:“您需要將XmlChoiceIdentifierAttribute添加到成員中”

C#
元芳怎么了 2022-08-20 17:07:02
我正在嘗試使用來更改將我的類序列化為 XML 的方式。我需要排除某些屬性,并按特定順序包含其他屬性。XmlAttributeOverrides我在這里有這個代碼:// XML Attribute Overrridespublic static XmlAttributeOverrides GetXMLAttributeOverrides(Type theType, List<string> propertiesToInlcudeInOrder, List<string> allColumnNames){    try    {        if (propertiesToInlcudeInOrder != null)        {            XmlAttributeOverrides theXMLAttributeOverrides = new XmlAttributeOverrides();            if (propertiesToInlcudeInOrder.Count > 0)            {                XmlAttributes mainNewXMLAttributes = new XmlAttributes();                mainNewXMLAttributes.XmlIgnore = false;                XmlAttributes ignoreXMLAttributes = new XmlAttributes();                ignoreXMLAttributes.XmlIgnore = true;                List<string> propertiesToNotInclude = new List<string>();                foreach (string theColumnName in allColumnNames)                {                    string thePropertyName = theColumnName;                    bool addProperty = true;                    foreach (string propertyToInclude in propertiesToInlcudeInOrder)                    {                        if (thePropertyName == propertyToInclude)                        {                            addProperty = false;                            break;                        }                    }                    if (addProperty)                    {                        propertiesToNotInclude.Add(thePropertyName);                    }                }
查看完整描述

1 回答

?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

您的基本問題是,您正在為每個屬性添加多個覆蓋 [XmlElement] 屬性,因為您正在對所有屬性使用單個實例,這會累積為所有屬性定義的對象。mainNewXMLAttributesXmlElementAttribute


要解決此問題,您需要為循環中的每個屬性分配一個 fresh,如以下更正和簡化的版本所示:mainNewXMLAttributesforeach (var propertyNameToIncludeInOrder in propertiesToInlcudeInOrder)GetXMLAttributeOverrides()


public static partial class ReportingManipulation

{

    public static XmlAttributeOverrides GetXMLAttributeOverrides(Type theType, IList<string> propertiesToInlcudeInOrder)

    {

        var allProperties = theType.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance).Select(p => p.Name);

        return GetXMLAttributeOverrides(theType, propertiesToInlcudeInOrder, allProperties);

    }


    // XML Attribute Overrrides

    public static XmlAttributeOverrides GetXMLAttributeOverrides(Type theType, IList<string> propertiesToInlcudeInOrder, IEnumerable<string> allProperties)

    {

        if (propertiesToInlcudeInOrder == null || propertiesToInlcudeInOrder.Count == 0)

            return null;


        var theXMLAttributeOverrides = new XmlAttributeOverrides();


        // To Add In Order

        int counter = 1;

        foreach (var propertyNameToIncludeInOrder in propertiesToInlcudeInOrder)

        {

            // Allocate a fresh instance of XmlAttributes for each property, because we are defining a different

            // XmlElementAttribute for each

            var mainNewXMLAttributes = new XmlAttributes { XmlIgnore = false };


            // Specify the element order XmlElementAttribute and attach to the XmlAttributes

            var theXMLElementAttributeToAdd = new XmlElementAttribute { Order = counter };

            mainNewXMLAttributes.XmlElements.Add(theXMLElementAttributeToAdd);


            // Attach the override XmlElementAttribute to the property propertyNameToIncludeInOrder

            theXMLAttributeOverrides.Add(theType, propertyNameToIncludeInOrder, mainNewXMLAttributes);


            counter++;

        }


        // To Ignore

        // Using System.Linq.Enumerable.Except()

        var propertiesToNotInclude = allProperties.Except(propertiesToInlcudeInOrder);

        var ignoreXMLAttributes = new XmlAttributes { XmlIgnore = true };

        foreach (var propertyNameToNotInlcude in propertiesToNotInclude)

        {

            // Attach the override XmlElementAttribute to the property propertyNameToIncludeInOrder

            // No need to allocate a fresh instance of ignoreXMLAttributes for each, because the instances would all be identical

            theXMLAttributeOverrides.Add(theType, propertyNameToNotInlcude, ignoreXMLAttributes);

        }


        return theXMLAttributeOverrides;

    }

}

為什么你的代碼不起作用?在初始代碼中,您需要執行以下操作:


XmlAttributes mainNewXMLAttributes = new XmlAttributes();

mainNewXMLAttributes.XmlIgnore = false;


int counter = 1;

foreach (string propertyNameToIncludeInOrder in propertiesToInlcudeInOrder)

{

    XmlElementAttribute theXMLElementAttributeToAdd = new XmlElementAttribute(propertyNameToIncludeInOrder);

    theXMLElementAttributeToAdd.ElementName = propertyNameToIncludeInOrder;

    theXMLElementAttributeToAdd.Order = counter;

    mainNewXMLAttributes.XmlElements.Add(theXMLElementAttributeToAdd);


    theXMLAttributeOverrides.Add(theType, propertyNameToIncludeInOrder, mainNewXMLAttributes);


    counter++;

}

現在,方法 XmlAttributeOverrides.Add(Type, String, XmlAttributes) 被記錄為按如下方式工作:


將對象添加到對象集合中。該參數指定要重寫的對象。該參數指定被覆蓋的成員的名稱。XmlAttributesXmlAttributestypemember


因此,當最終構造 時,的內容將應用于命名參數。當您為所有參數僅構造 實例時,其數組將包含與所有參數對應的元素名稱!即,您的代碼嘗試將多個屬性應用于每個命名參數,僅在覆蓋名稱和順序上有所不同。這說明了您需要將 XmlChoiceIdentifierAttribute 添加到 'TextColumn' 成員的異常 -- 如果屬性值是多態的,并且您希望將不同的元素名稱分配給不同的值類型,則只能將多個元素名稱附加到屬性。mainNewXMLAttributesXmlSerializermainNewXMLAttributesXmlElements[XmlElement]


筆記


生成 with 覆蓋時,必須靜態緩存它,并在以后重用它以避免嚴重的內存泄漏,如使用 StreamReader 和 XmlSerializer 的內存泄漏中所述。XmlSerializer


我不建議無條件地吞下異常,并在低級實用工具方法或對象構造函數中將它們作為錯誤消息呈現給用戶。


在這里演示工作小提琴。


查看完整回答
反對 回復 2022-08-20
  • 1 回答
  • 0 關注
  • 178 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號