ASP.NET Core XML model binding

iKingNinja 100 Reputation points
2024-11-22T18:14:29.01+00:00

I'm trying to receive data in XML format in my controller action. However I'm not sure about how to create a model for the following XML data.

I want model binding to add all text tags to a list and do the same thing for card tags.

<?xml version="1.0" encoding="UTF-8"?>
<XmlUpdatePortfolioContentRequest>
	<schema-version>string</schema-version>
	<portfolio>
		<text>Hello World!</text>

		<card>
			<place-id>0</place-id>
			<work-period begin="2024-11-22" end="2024-12-10" />
			<role>string</role>
		</card>

		<card>
			<place-id>0</place-id>
			<work-period begin="2024-11-22" end="2024-12-10" />
			<role>string</role>
		</card>

		<text centered="true" header="1">Hello World! 2</text>
	</portfolio>
</XmlUpdatePortfolioContentRequest>
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,668 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 28,786 Reputation points
    2024-11-22T19:52:16.4466667+00:00

    Use Visual Studio to create the model. First, copy the XML then in Visual Studio click Edit -> Paste Special -> Paste XML as classes.

    
    // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class XmlUpdatePortfolioContentRequest
    {
    
        private string schemaversionField;
    
        private XmlUpdatePortfolioContentRequestPortfolio portfolioField;
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("schema-version")]
        public string schemaversion
        {
            get
            {
                return this.schemaversionField;
            }
            set
            {
                this.schemaversionField = value;
            }
        }
    
        /// <remarks/>
        public XmlUpdatePortfolioContentRequestPortfolio portfolio
        {
            get
            {
                return this.portfolioField;
            }
            set
            {
                this.portfolioField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class XmlUpdatePortfolioContentRequestPortfolio
    {
    
        private object[] itemsField;
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("card", typeof(XmlUpdatePortfolioContentRequestPortfolioCard))]
        [System.Xml.Serialization.XmlElementAttribute("text", typeof(XmlUpdatePortfolioContentRequestPortfolioText))]
        public object[] Items
        {
            get
            {
                return this.itemsField;
            }
            set
            {
                this.itemsField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class XmlUpdatePortfolioContentRequestPortfolioCard
    {
    
        private byte placeidField;
    
        private XmlUpdatePortfolioContentRequestPortfolioCardWorkperiod workperiodField;
    
        private string roleField;
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("place-id")]
        public byte placeid
        {
            get
            {
                return this.placeidField;
            }
            set
            {
                this.placeidField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("work-period")]
        public XmlUpdatePortfolioContentRequestPortfolioCardWorkperiod workperiod
        {
            get
            {
                return this.workperiodField;
            }
            set
            {
                this.workperiodField = value;
            }
        }
    
        /// <remarks/>
        public string role
        {
            get
            {
                return this.roleField;
            }
            set
            {
                this.roleField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class XmlUpdatePortfolioContentRequestPortfolioCardWorkperiod
    {
    
        private System.DateTime beginField;
    
        private System.DateTime endField;
    
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute(DataType = "date")]
        public System.DateTime begin
        {
            get
            {
                return this.beginField;
            }
            set
            {
                this.beginField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute(DataType = "date")]
        public System.DateTime end
        {
            get
            {
                return this.endField;
            }
            set
            {
                this.endField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class XmlUpdatePortfolioContentRequestPortfolioText
    {
    
        private bool centeredField;
    
        private bool centeredFieldSpecified;
    
        private byte headerField;
    
        private bool headerFieldSpecified;
    
        private string valueField;
    
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public bool centered
        {
            get
            {
                return this.centeredField;
            }
            set
            {
                this.centeredField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool centeredSpecified
        {
            get
            {
                return this.centeredFieldSpecified;
            }
            set
            {
                this.centeredFieldSpecified = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public byte header
        {
            get
            {
                return this.headerField;
            }
            set
            {
                this.headerField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool headerSpecified
        {
            get
            {
                return this.headerFieldSpecified;
            }
            set
            {
                this.headerFieldSpecified = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public string Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }
    
    
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.