2

I have a xml item tag which should be mapped into List. But while mapping apart from the list of item other elements has been mapped to the model.

I referred to other questions where each items is given a parent so they mapped while deserialising.

Let me know how to map to the given list item into the respective model.

XML:

<SecretModel>
    <id>3181</id>
    <name>Test</name>
    <secretTemplateId>6044</secretTemplateId>
    <folderId>674</folderId>
    <active>true</active>
    <items>
        <itemId>13960</itemId>
        <fileAttachmentId/>
        <filename/>
        <itemValue>Test</itemValue>
        <fieldId>287</fieldId>
        <fieldName>Username</fieldName>
        <slug>username</slug>
        <fieldDescription>The Amazon IAM username.</fieldDescription>
        <isFile>false</isFile>
        <isNotes>false</isNotes>
        <isPassword>false</isPassword>
    </items>
    <items>
        <itemId>13961</itemId>
        <fileAttachmentId/>
        <filename/>
        <itemValue>AKIAU5G4MQBA2TKQOWNW</itemValue>
        <fieldId>284</fieldId>
        <fieldName>Access Key</fieldName>
        <slug>access-key</slug>
        <fieldDescription>The Amazon IAM access key.</fieldDescription>
        <isFile>false</isFile>
        <isNotes>false</isNotes>
        <isPassword>false</isPassword>
    </items>
</secretModel>

SecretModel.cs:
   
   Public class SecretModel
   {
    public int? id {get; set;}
    public string name {get; set;}
    public string SecretTemplateID {get; set;}
    public string folderId {get; set;}
    public string active {get; set;}
    public string List<RestSecretItem> {get; set;}
    }   
    
    
    public partial class RestSecretItem : IEquatable<RestSecretItem>
    { 
        
        public string FieldDescription { get; set; }

        public int? FieldId { get; set; }

        public string FieldName { get; set; }

        public int? FileAttachmentId { get; set; }
        public string Filename { get; set; }
        public bool? IsFile { get; set; }

        public bool? IsNotes { get; set; }

        public bool? IsPassword { get; set; }
        public int? ItemId { get; set; }

        public string ItemValue { get; set; }

        public string Slug { get; set; }
}

1 Answer 1

2

There are many ways to map xml, one is using built-in .NET XML serializer. Here is one way to serialize it.

SecretModel

[XmlRoot(ElementName = "SecretModel")]
public class SecretModel 
{
   [XmlAttribute(AttributeName = "id")]
   public int Id { get; set; }
   [XmlAttribute(AttributeName = "name")]
   public string Name { get; set; }
   [XmlAttribute(AttributeName = "secretTemplateId")]
   public string SecretTemplateId { get; set; }
   [XmlAttribute(AttributeName = "folderId")]
   public string FolderId { get; set; }
   [XmlAttribute(AttributeName = "active")]
   public bool Active { get; set; }
   [XmlElement(ElementName = "items")]
   public List<RestSecretItem> Items { get; set; }
}

RestSecretItem

[XmlRoot(ElementName = "items")]
public class RestSecretItem
{
   [XmlAttribute(AttributeName = "itemId")]
   public int ItemId { get; set; }
   [XmlAttribute(AttributeName = "fileAttachmentId")]
   public int FileAttachmentId { get; set; }
   [XmlAttribute(AttributeName = "filename")]
   public string FileName { get; set; }
   [XmlAttribute(AttributeName = "itemValue")]
   public string ItemValue { get; set; }
   [XmlAttribute(AttributeName = "fieldId")]
   public int FieldId { get; set; }
   [XmlAttribute(AttributeName = "fieldName")]
   public string FieldName { get; set; }
   [XmlAttribute(AttributeName = "slug")]
   public string Slug { get; set; }
   [XmlAttribute(AttributeName = "fieldDescription")]
   public string FieldDescription { get; set; }
   [XmlAttribute(AttributeName = "isFile")]
   public bool IsFile { get; set; }
   [XmlAttribute(AttributeName = "isNotes")]
   public bool IsNote { get; set; }
   [XmlAttribute(AttributeName = "isPassword")]
   public bool IsPassword { get; set; }
}

Now you can parse the xml through using this code:

var doc = new XmlDocument();
doc.load("your directory or path of file here");
using(var reader = new StringReader(doc.InnerXml)) 
{
   var serializer = new XmlSerializer(typeof(SecretModel));
   var data = (SecretModel)serializer.Deserialize(reader); // converted model here
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.