简体   繁体   中英

How to serialize a class to an xml structure?

How could I serialize my below class {0} to the below xml {1}.

So, the class name, property names should match to the xml.

{0}:

[Serializable]
public class ProfileSite
{
    [XmlAttribute("profileId")] 
    public int ProfileId { get; set; }

    [XmlAttribute("siteId")] 
    public int SiteId { get; set; }

    public Link[] Links { get; set; }

    public XElement Deserialize()
    {

    }
}

{1}:

<profileSite profileId="" siteId="">
    <links>
      <link>
        <originalUrl></originalUrl>
        <isCrawled></isCrawled>
        <isBroken></isBroken>
        <isHtmlPage></isHtmlPage>
        <firstAppearedLevel></firstAppearedLevel>
      </link>
    </links>
  </profileSite>

Many thanks,

[XmlRoot("profileSite")]
public class ProfileSite
{
    [XmlAttribute("profileId")] 
    public int ProfileId { get; set; }

    [XmlAttribute("siteId")] 
    public int SiteId { get; set; }

    [XmlArray("links"), XmlArrayItem("link")]    
    public Link[] Links { get; set; }
}

then:

var ser = new XmlSerializer(typeof(ProfileSite));
var site = (ProfileSite) ser.Deserialize(source);

The first step is to mark up your class with the relevant Xml... attributes which control the sreialization and whether to have attributes or elements. Your requirement basically changes the case, and has properties of the main object as attributes and properties of all child Link objects as elements:

[XmlRoot("profileSite")]
public class ProfileSite
{
    [XmlAttribute("profileId")] 
    public int ProfileId { get; set; }

    [XmlAttribute("siteId")] 
    public int SiteId { get; set; }

    [XmlArray("links"), XmlArrayItem("link")]    
    public Link[] Links { get; set; }

}

public class Link
{
    [XmlElement("originalUrl")]
    public string OriginalUrl{get;set;}
    // You other props here much like the above
}

Then to serialize it use XmlSerializer.Serialize there are many overloads taking varios places to output the result. For testing you can use the Console.Out .

XmlSerializer serializer = new XmlSerializer(typeof(ProfileSite));
serializer.Serialize(Console.Out, obj);

You may want to add an empty namespace manager, which stops the ugly extra xmlns attributes:

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("","");
XmlSerializer serializer = new XmlSerializer(typeof(ProfileSite));
serializer.Serialize(Console.Out, obj,ns);

Output of the above using this example object:

var obj = new ProfileSite{
            ProfileId=1,
            SiteId=2,
            Links = new[]{ 
                new Link{OriginalUrl="www.google.com" },
                new Link{OriginalUrl="www.foo.com" }
            }};

is this:

<?xml version="1.0" encoding="utf-8"?>
<profileSite profileId="1" siteId="2">
  <links>
    <link>
      <originalUrl>www.google.com</originalUrl>
    </link>
    <link>
      <originalUrl>www.foo.com</originalUrl>
    </link>
  </links>
</profileSite>

Finally, here's a working example for you to play around with: http://rextester.com/XCJHD55693

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM