locked
Avoid duplicate entry in xml file c# RRS feed

  • Question

  • User-317791795 posted

    Hi,

    I am trying to restrict duplicate entry to an XML file and below is the XML file.

    <?xml version="1.0" standalone="yes"?>
    <Info>
    <Details>
    <ID>Ryan</ID>
    </Details>
    <Details>
    <ID>Danny</ID>
    </Details>
    </Info>

    Now if I try to add Ryan or Danny again to the ID I should alert like user name already exists.

    I'm using the below code and it doesn't work. 

    strName is a string and has username value to be added. Can anyone provide suggestions?
    XDocument xDoc = XDocument.Load(Server.MapPath("~/Info.xml"));
            bool userExistsAlready = xDoc.Descendants("Details").Any(x => (string)x.Attribute("ID") == strName);
            if (userExistsAlready)
            {
                //alert
            }
    Saturday, October 22, 2016 9:27 AM

All replies

  • User-183374066 posted

    Try following working example

    using System;
    using System.IO;
    using System.Linq;
    using System.Xml.Serialization;
    
    namespace JobGarden.TestApp
    {
    
    
        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
        public partial class Info
        {
    
            private InfoDetails[] detailsField;
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("Details")]
            public InfoDetails[] Details
            {
                get
                {
                    return this.detailsField;
                }
                set
                {
                    this.detailsField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class InfoDetails
        {
    
            private string idField;
    
            /// <remarks/>
            public string ID
            {
                get
                {
                    return this.idField;
                }
                set
                {
                    this.idField = value;
                }
            }
        }
    
    
        class Program
        {
            static void Main(string[] args)
            {
    
                string fileName = @"D:\JobGarden\Code\JobGarden\JobGarden.TestApp\Info.xml";
    
                XmlSerializer deserializer = new XmlSerializer(typeof(Info));
                TextReader textReader = new StreamReader(fileName);
                Info result = (Info)deserializer.Deserialize(textReader);
                textReader.Close();
    
                string name = "Ryan";
    
                var infoDetail = result.Details.FirstOrDefault(x => x.ID.ToLower().Equals(name.ToLower()));
                if (infoDetail == null)
                {
                    Console.WriteLine(name + " doesn't exists");
                }
                else
                {
                    Console.WriteLine(infoDetail.ID + " does exists");
                }
    
                string name2 = "Vicky";
    
                var infoDetail2 = result.Details.FirstOrDefault(x => x.ID.ToLower().Equals(name2.ToLower()));
                if (infoDetail2 == null)
                {
                    Console.WriteLine(name2 + " doesn't exists");
                }
                else
                {
                    Console.WriteLine(infoDetail2.ID + " does exists");
                }
    
                Console.ReadKey();
            }
        }
    
       
    
    }
    

    Know how to generate classes from xml

    http://blog.codeinside.eu/2014/09/08/Visual-Studio-2013-Paste-Special-JSON-And-Xml/

    Saturday, October 22, 2016 12:25 PM
  • User303363814 posted

    ID is an element of "Details" (not an Attribute).  Change the .Any clause to

    .Any(x => x.Element("ID").Value == strName)

    Sunday, October 23, 2016 11:43 PM
  • User1724605321 posted

    Hi vickyt5,

    Seems your question is answered on S.O,  here is the solution :

    http://stackoverflow.com/a/40190486/5751404 

    Best Regards,

    Nan Yu

    Monday, October 24, 2016 7:35 AM
  • User303363814 posted

    Seems your question is answered on S.O
    Seems it is also answered right here by me!

    Monday, October 24, 2016 9:35 AM