Search code examples
c#xmllinq-to-xmlxml-serialization

XDocument Elements - Remove tags


I'm currently importing the values from a valid XML file into a SQL database. Below is a snippet of the xml.

<Suppliers>
    <SupplierDetails>
      <Name>Some Name</Name>
      <Logo>my-logo.jpg</Logo>
      <ID>4070</ID>
      <DateCertified>14/10/2016 00:00:00</DateCertified>
      <Description>Some description</Description>
      <Brands>Brand1</Brands>
      <Brands>Brand2</Brands>
      <Brands>Brand3</Brands>
      <Brands>Brand4</Brands>
      <SubBrands>SubBrand1</SubBrands>
      <SubBrands>SubBrand2</SubBrands>
      <SubBrands>SubBrand3</SubBrands>
      <SubBrands>HVAC</SubBrands>
    </SupplierDetails>
</Suppliers>

The issue i'm having is with the list of Brands and SubBrands. All other values such as Name, Description etc are fine and are returned without their element "tags". However, the Brands / SubBrands look like this.

  <Brands>Brand1</Brands>

I just need the "Brand1" bit.

This is how i'm binding my ViewModel

var model =
  from xml in xmlDoc.Descendants("SupplierDetails")
            select new SupplierViewModel
            {                    
                Name            = xml.Element("Name").ToString(),
                Logo            = xml.Element("Logo").ToString(),
                Supplier_Id     = (int)xml.Element("ID"),
                DateCertified   = xml.Element("DateCertified").ToString(),
                Description     = xml.Element("Description").ToString(),
                Brands          = (from b in xml.Elements("Brands") select b.ToString()).ToList(),
                SubBrands       = (from sb in xml.Elements("SubBrands") select sb.ToString()).ToList()
            };

And my ViewModel

public class SupplierViewModel
{
    public int Id { get; set; }

    // XML Elements
    [XmlElement("Name")]
    public string Name { get; set; }

    [XmlElement("LOGO")]
    public string Logo { get; set; }

    [XmlElement("ID")]
    public int Supplier_Id { get; set; }

    [XmlElement("DateCertified")]
    public string DateCertified { get; set; }

    [XmlElement("Description")]
    public string Description { get; set; }       

    [XmlElement("Brands")]
    public List<string> Brands { get; set; }

    [XmlElement("SubBrands")]
    public List<string> SubBrands { get; set; }
}

I know i can use a bit of RegEx to remove the tags but is there any other way of doing this on-the-fly...?

Thanks in advance. :)


Solution

  • Should be enough to read the .Value instead of using .ToString()

        string xmlString = "<Suppliers><SupplierDetails><Name>Some Name</Name><Brands>Brand1</Brands><Brands>Brand2</Brands></SupplierDetails></Suppliers>";
        var xmlDoc = XDocument.Parse(xmlString);
        var model =
          from xml in xmlDoc.Descendants("SupplierDetails")
          select new SupplierViewModel {
              Name = xml.Element("Name").ToString(),
              Brands = (from b in xml.Elements("Brands") select b.Value).ToList(),
          };
        var YourModel = model.ToList()[0];