代码之家  ›  专栏  ›  技术社区  ›  char m

序列化数组时如何使用xmlAttributeOverrides?

  •  6
  • char m  · 技术社区  · 15 年前

    我有一个名为“updatedcomponents”的数组,它是属于类networkcomponent的对象。我必须以更改根元素(=array)的名称和命名空间以及将单个networkcomponent项的名称更改为component的方式对其进行序列化。我下面有一个导致异常的代码:

    System.InvalidOperationException:反射类型“ComponentSyncService.NetworkComponent[]”时出错。--->system.invalidOperationException:不能为类型componentSyncService.networkcomponent[]指定xmlRoot和xmlType属性。

    代码:

    XmlAttributeOverrides xaos = new XmlAttributeOverrides();
    
    // the array itself aka the root. change name and namespace
    XmlElementAttribute xea = new XmlElementAttribute(_updatedComponents.GetType());
    xea.Namespace = "http://www.example.com/nis/componentsync";
    xea.ElementName = "components";
    
    XmlAttributes xas = new XmlAttributes();
    xas.XmlElements.Add(xea);
    xaos.Add(_updatedComponents.GetType(), xas); 
    
    // then the items of the array. just change the name
    xea = new XmlElementAttribute(typeof(networkcomponent));
    xea.ElementName = "component";
    
    xas = new XmlAttributes();
    xas.XmlElements.Add(xea);
    xaos.Add(typeof(NetworkComponent), "NetworkComponent", xas);
    
    XmlSerializer serializer = new XmlSerializer(_updatedComponents.GetType(), xaos);
    
    XmlTextWriter writer = new XmlTextWriter(string.Format("{0}\\ComponentSyncWS_{1}.xml", 
                          Preferences.FileSyncDirectory, requestId), Encoding.UTF8);
    serializer.Serialize(writer, _updatedComponents);
    
    1 回复  |  直到 8 年前
        1
  •  8
  •   Kenster marc_s    8 年前

    是什么 _updatedComponents 是吗?我猜这是 NetworkComponent[]

    public class ComponentsMessage {
        public NetworkComponent[] Components {get;set;}
    }
    

    NetworkComponent ComponentsMessage

    [XmlRoot("components", Namespace = XmlNamespace)]
    [XmlType("components", Namespace = XmlNamespace)]
    public class ComponentsMessage
    {
        public const string XmlNamespace = "http://www.example.com/nis/componentsync";
        [XmlElement("component")]
        public NetworkComponent[] Components { get; set; }
    }
    

    public class ComponentsMessage
    {
        public NetworkComponent[] Components { get; set; }
    }
    class Program
    {
        static void Main()
        {
            NetworkComponent[] _updatedComponents = new NetworkComponent[2] {
                new NetworkComponent{},new NetworkComponent{}
            };
            const string XmlNamespace = "http://www.example.com/nis/componentsync";
            XmlAttributeOverrides ao = new XmlAttributeOverrides();
            ao.Add(typeof(ComponentsMessage), new XmlAttributes {
                XmlRoot = new XmlRootAttribute("components") { Namespace = XmlNamespace },
                XmlType = new XmlTypeAttribute("components") { Namespace = XmlNamespace }
            });
            ao.Add(typeof(ComponentsMessage), "Components", new XmlAttributes {
                XmlElements =  {
                    new XmlElementAttribute("component")
                }
            });
            ComponentsMessage msg = new ComponentsMessage { Components = _updatedComponents };
            XmlSerializer serializer = new XmlSerializer(msg.GetType(), ao);
            serializer.Serialize(Console.Out, msg);
        }
    }