开发者

xml serialization arrays C#

i was faced with one that got me bad

<items>
<name ="something 1" />

<subitems>
<name ="sub something1" />
<item name="item 1" />
<item name="item 2" />
<item name="item 3" />
<someOtherTag name="Come On Really">
</subitems>

</items>

I am curently serializing an object and i came up with this one开发者_运维知识库 last structure that threw me for a loop. I really wanted to use xmlarray attribute on the subitems but the extra name tag threw me off. Any ideas? I am looking for a C# de-serialization specifically.


Since your XML as posted is invalid, we need to get it valid first. It can't be deserialized if it can't be parsed. If this "XML" is coming from an outside source, they need to first provide you with a syntactically valid XML document. If the "XML" is generated by something under your control, you need to fix it.

Here are a couple forms of similar markup that are syntactically valid XML, and are easily serializable via XML Serialization using nothing more than simple attributes:

  • Option 1:

    <items name ="something 1" >
      <subitems name ="sub something1" >
        <item name="item 1" />
        <item name="item 2" />
        <item name="item 3" />
        <someOtherTag name="Come On Really" />
      </subitems>
    </items>
    

    For this option, here's a class that will serialize/deserialize the above markup (one should note that, as they say in the Perl world, "TMTOWTDI" (There's more than one way to do it):

      [XmlRoot( "items" )]
      public class Widget
      {
    
        [XmlAttribute("name")]
        public string Name { get ; set ; }
    
        [XmlElement( "subitems" )]
        public SubWidget SubItems { get ; set ; } 
    
        public class SubWidget
        {
    
          [XmlAttribute("name")]
          public string Name { get ; set ; }
    
          [XmlElement("item")]
          public Element[] Item { get ; set ; }
    
          [XmlElement("someOtherTag")]
          public Element SomeOtherTag { get ; set ; }
    
          public class Element
          {
    
            [XmlAttribute("name")]
            public string Name { get ; set ; }
    
          }
    
        }
    
      }
    
  • Option 2:

    <items>
      <name>something 1</name>
      <subitems>
        <name>sub something1</name>
        <item name="item 1" />
        <item name="item 2" />
        <item name="item 3" />
        <someOtherTag name="Come On Really" />
      </subitems>
    </items>
    

    Again, a class that will serialize/deseraliaze this markup is virtually identical to the first case. All we do is change two attributes from XmlAttribute to XmlElement, thus:

    [XmlRoot( "items" )]
    public class Widget
    {
    
      [XmlElement("name")] // was XmlAttribute
      public string Name { get ; set ; }
    
      [XmlElement( "subitems" )]
      public SubWidget SubItems { get ; set ; } 
    
      public class  SubWidget
      {
    
        [XmlElement("name")] // was XmlAttribute
        public string Name { get ; set ; }
    
        [XmlElement("item")]
        public Element[] Item { get ; set ; }
    
        [XmlElement("someOtherTag")]
        public Element SomeOtherTag { get ; set ; }
    
        public class Element
        {
    
          [XmlAttribute("name")]
          public string Name { get ; set ; }
        }
    
      }
    
    }
    


This isn't really valid xml to start.

You should place an <items> tag around your repeating series of items.

Also, try using the built in .Net serializers:

The easiest way to populate a collection from XML file in .NET

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜