// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.IO;
using System.Xml.Linq;

namespace Sample.Models
{
    public partial class TestXmlModel : global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.TestXmlModel>
    {
        internal TestXmlModel()
        {
        }

        protected virtual global::Sample.Models.TestXmlModel PersistableModelCreateCore(global::System.BinaryData data, global::System.ClientModel.Primitives.ModelReaderWriterOptions options)
        {
            string format = (options.Format == "W") ? ((global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.TestXmlModel>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "X":
                    using (global::System.IO.Stream dataStream = data.ToStream())
                    {
                        return global::Sample.Models.TestXmlModel.DeserializeTestXmlModel(global::System.Xml.Linq.XElement.Load(dataStream, global::System.Xml.Linq.LoadOptions.PreserveWhitespace), options);
                    }
                default:
                    throw new global::System.FormatException($"The model {nameof(global::Sample.Models.TestXmlModel)} does not support reading '{options.Format}' format.");
            }
        }

        internal static global::Sample.Models.TestXmlModel DeserializeTestXmlModel(global::System.Xml.Linq.XElement element, global::System.ClientModel.Primitives.ModelReaderWriterOptions options)
        {
            if ((element == null))
            {
                return null;
            }

            string name = default;

            foreach (var child in element.Elements())
            {
                string localName = child.Name.LocalName;
                if ((localName == "name"))
                {
                    name = ((string)child);
                    continue;
                }
            }
            return new global::Sample.Models.TestXmlModel(name);
        }
    }
}
