Show correct message based on entering URL in C#


If user entering url across multiple country wise, if url exists message should come like : facebook.com exists in DE,IN. Here is the current code

public JsonMessage SaveReview(Review review, string role, string userName) {
  try {
   if (review != null && !string.IsNullOrEmpty(role)) {

    string domain = StringUtils.ExtractDomainExtOnly(StringUtils.GetValidUrl(review.URL));
    string xmlFilePath = string.Empty;
    string listElement = string.Empty;
    string lang = string.Empty;
    string url = string.Empty;
    string domainPubCreator = string.Empty;


    if (role.ToUpper() == RoleEnums.Role.PUBCREATOR.ToString().ToUpper()) {
     string[] language = review.Language.Split(new char[] {
      ','
     });

     string[] URL = review.URL.Split(new char[] {
      ','
     });

     string DuplicateLang = string.Empty;
     string OtherLang = string.Empty;
     string DuplicateUrl = string.Empty;
     string otherUrl = string.Empty;
     StringBuilder sb = new StringBuilder();
     bool isExixstURL = false;
     bool IsUrlSave = false;


     string OtherSameUrl = string.Empty;
     string OtherSameLang = string.Empty;

     for (var i = 0; i < language.Length; i++) {
      lang = language[i];
      xmlFilePath = B2lConstants.PhyPath + @ "xml_Data/ReviewLinks_" + lang + ".xml";
      if (File.Exists(xmlFilePath)) {
       reviewData = XDocument.Load(xmlFilePath);
       if (reviewData != null) {
        XElement siteElement = null;


        if (review.ID == 0) {
         int lastId = 0;


         for (var j = 0; j < URL.Length; j++) {

          url = URL[j];

          domainPubCreator = StringUtils.ExtractDomainExtOnly(StringUtils.GetValidUrl(url));

          var dataURL = reviewData.Element("REVIEWLINKS").Descendants("SITE").Where(x => x.Attribute("isDelete").Value == "false" && x.Attribute("id").Value != review.ID.ToString()).Descendants("DOMAIN").Where(x => x.Value.ToLower() == domainPubCreator.ToLower());

          if (dataURL != null && dataURL.Count() > 0) {
           isExixstURL = true;
           if (DuplicateUrl == "") {

            DuplicateUrl = url;
            DuplicateLang = lang;

           } else if (DuplicateUrl != "") {
            DuplicateUrl = DuplicateUrl + "," + url;
            DuplicateLang = DuplicateLang + "," + lang;
           }

          } else {
           isExixstURL = false;
           if (otherUrl == "") {
            otherUrl = url;
            OtherLang = lang;
            OtherSameUrl = otherUrl;
            OtherSameLang = OtherLang;
           } else if (otherUrl != "") {
            otherUrl = otherUrl + "," + url;
            OtherLang = OtherLang + "," + lang;
           }
          }

          //insert
          if (!isExixstURL) {
           IsUrlSave = true;
           _jsonMessage = new JsonMessage(true, Resources.Resources.lbl_success, Resources.Resources.lbl_saveSuccessfully, KeyEnums.JsonMessageType.SUCCESS);
          } else {
           _jsonMessage = new JsonMessage(false, Resources.Resources.lbl_attention, "Domain ''" + domainPubCreator + "'' already exists!", KeyEnums.JsonMessageType.WARNING);
          }
         }
        } else
       }
      }

     }

     continue;
    }


    if (DuplicateUrl != "" && IsUrlSave) {
     sb.AppendLine("Domain ''" + DuplicateUrl + "'' already exists in language ''" + DuplicateLang + "''");

     sb.AppendLine("URL : '" + otherUrl + "' saved successfully in language ''" + OtherLang + "''");
     _jsonMessage = new JsonMessage(true, Resources.Resources.lbl_success, sb.ToString(), KeyEnums.JsonMessageType.SUCCESS);
    } else if (DuplicateUrl != "") {
     _jsonMessage = new JsonMessage(false, Resources.Resources.lbl_attention, "Domain ''" + DuplicateUrl + "'' already exists in language ''" + DuplicateLang + "''", KeyEnums.JsonMessageType.WARNING);
    } else if (DuplicateUrl == "") {
     _jsonMessage = new JsonMessage(true, Resources.Resources.lbl_success, Resources.Resources.lbl_saveSuccessfully, KeyEnums.JsonMessageType.SUCCESS);
    }

   }
   return _jsonMessage;
  }

I am storing multiple url countrywise. What I need here is like for example:

1. if user enters facebook.com and select country DE,IN then if url exists already , messge should come facebook.com already exists in DE,IN.

But in my above code am getting message like : facebook.com,facebook.com already exists in DE,IN

2.Enter URL Like this : www.amazon.in,www.facebook44.in,www.facebook45.in 

Selected country : FR,PL,IT 

Message should be : www.facebook44.in,www.facebook45.in saved in FR.

www.amazon.in already exists in FR.

www.facebook44.in,www.facebook45.in already exists in PL,IT.

www.amazon.in saved in PL,IT.

 

 

Please, suggest me the way to show messages like this .Thanks .


Asked by:- SnehalSawant
0
: 2549 At:- 4/22/2018 1:27:21 PM
asp.net c#

Can you enter just specific details with the only code which is checking URL and passing message in database/XML or wherever you are storing it, paste the demo code, which can be debugged by other users.Clean/important code lines would be better, thanks Also, mention paramaters value you are passing to function 0
By : manish - at :- 4/22/2018 4:54:17 PM






2 Answers
profileImage Answered by:- neena

As your information is limited I have created my own demo XML & C# function, you can read the comments to understand

Here is the XML file I am using for demo

<?xml version="1.0" encoding="utf-8" ?> 
<REVIEWLINKS>
  <SITE isDelete="false" Url="www.facebook.com" country="USA"></SITE>
  <SITE isDelete="false" Url="www.facebook.com" country="IN"></SITE>
  <SITE isDelete="false" Url="www.facebook.com" country="SA"></SITE>
  <SITE isDelete="false" Url="www.google.com" country="SA"></SITE>
  <SITE isDelete="false" Url="www.google.com" country="IN"></SITE>
</REVIEWLINKS>

and here is the function which checks for URL, I am not passing dynamic parameters data but it should work for dynamic parameters also, as only values will be changed.

 public JsonResult XMLSearch()
        {
            var URL = "www.facebook.com,www.google.com";
            var Countries = "SA,IN";

            //Split URLs and COuntries
            var URLs = URL.Split(',');
            var Country = Countries.Split(',');

            //Initialize empty string
            var Msg = "";
            foreach (var url in URLs)
            {
                //create a country URL in a loop
                var CountryURLAdded = false;

                foreach (var Con in Country)
                {
                    XDocument doc = XDocument.Load(Server.MapPath("~/Test.xml"));
                    var addresses = doc.Element("REVIEWLINKS").Descendants("SITE").Where(x => x.Attribute("isDelete").Value == "false" && x.Attribute("Url").Value == url && x.Attribute("country").Value == Con).FirstOrDefault();

                    //check if URL exists in Country
                    if (addresses != null)
                    {
                        if (Msg == "")
                        {
                            //URL is not added in string
                            if (!CountryURLAdded)
                            {
                                Msg = url + " exists in " + Con;

                                //To check if URL is added in string with atleast one country
                                CountryURLAdded = true;
                            }
                           
                        }
                        //already exists URL in string then just append country
                        else if (Msg != "" && CountryURLAdded)
                        {
                            Msg = Msg + "-" + Con;
                        }
                        //New URL/Country but have already MSG string has data
                        else if (Msg != "")
                        {
                            //URL is not added in string
                            if (!CountryURLAdded)
                            {
                                Msg = Msg + ", " + url+" exists in " + Con;
                                CountryURLAdded = true;
                            }
                            
                           
                        }
                    }
                }
               
               

            }

           

            return Json(new { Message= Msg});
        }

After executing the above function for the demo XML and debugging it in Visual Studio, I was able to get output

output-url-fetch-XML-min.png

 

That's it, you just need to check If the string is empty or not, If yes, if website URL is in the Msg string or not and accordingly add the new country name/URL, there can be many alternatives of the same solution, depending on the coding logic you implement.

Also, You may need to change Code according to your XML and need.

3
At:- 4/23/2018 6:28:41 PM Updated at:- 4/23/2018 6:29:52 PM


profileImage Answered by:- neena

I tried executing your Function in my local Visual studio, but Wasn't able to succesfully do it, as i am not sure what is JSONMessage (return type in function), what are you really passing in Review?

Looking at your above code, I think you are seraching xml file for the data

 xmlFilePath = B2lConstants.PhyPath + @ "xml_Data/ReviewLinks_" + lang + ".xml";

Is it possible to provide demo XML file, with function paramerts value, so that i can debug your code and try to get output as required.

Because your output doesn't just depend on C#, it is also dependent on XML file and how you are searching the nodes.

0
At:- 4/23/2018 8:10:17 AM
Hi,Thanks. In review object, i am passing(comma separated url and country) user entered multiple url and selected multiple country .There is countrywise xml file , if url already exists in file , i am doing(isExistsurl = true) and fetching that into duplicate url variable. But for e.g. if facebook.com exists in 2 langauges ,message is like facebook.com,facebook.com exists in DE,IN. In short if there is common url it should appear as once and message be like : facebook.com exists in DE,IN. 0
By : SnehalSawant - at :- 4/23/2018 12:02:37 PM
Good to know, thanks, I have added an answer with my demo function, which should work for you also 0
By : neena - at :- 4/23/2018 6:30:18 PM
Thanks mam! ! ! ! 0
By : SnehalSawant - at :- 4/24/2018 5:44:30 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use