Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Here in the code below 4 is repeated and 2 is missing.
My requirement is that I want to automatically replace the repeated id with the missing word.


Hence My id should be ordered without any missing id.
Hoping you've understand my problem





XML
<Model name="7049_PIPING.sat" version="v2.0" unit="m" count="12240" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Color id="256" r="255" g="255" b="255" />
  <Component id="0" colorId="256" layer="0">
    </Component>
  <Component id="1" colorId="256" layer="0">
    </Component>
  <Component id="4" colorId="256" layer="0">
    </Component>
  <Component id="3" colorId="256" layer="0">
    </Component>
  <Component id="4" colorId="256" layer="0">
   </Component>
  <Component id="5" colorId="256" layer="0">
   </Component>
  <Component id="6" colorId="256" layer="0">
    </Component>
  <Component  id="8" colorId="256" layer="0">
   </Component>
  <Component id="8" colorId="256" layer="0">
    </Component>
  <Component id="9" colorId="256" layer="0">
   </Component>
  <Component id="10" colorId="256" layer="0">
    </Component>
</Model>






Expected Output:



XML
<pre lang="xml"><Model name="7049_PIPING.sat" version="v2.0" unit="m" count="12240" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Color id="256" r="255" g="255" b="255" />
  <Component id="0" colorId="256" layer="0">
    </Component>
  <Component id="1" colorId="256" layer="0">
    </Component>
  <Component id="2" colorId="256" layer="0">
    </Component>
  <Component id="3" colorId="256" layer="0">
    </Component>
  <Component id="4" colorId="256" layer="0">
   </Component>
  <Component id="5" colorId="256" layer="0">
   </Component>
  <Component id="6" colorId="256" layer="0">
    </Component>
  <Component  id="7" colorId="256" layer="0">
   </Component>
  <Component id="8" colorId="256" layer="0">
    </Component>
  <Component id="9" colorId="256" layer="0">
   </Component>
  <Component id="10" colorId="256" layer="0">
    </Component>
</Model>
Posted
Updated 5-Feb-14 21:55pm
v6
Comments
RhishikeshLathe 6-Feb-14 2:49am    
can u post ur output..
KUMAR619 6-Feb-14 3:54am    
I have added my expected output
RhishikeshLathe 6-Feb-14 4:28am    
same code that i have posted yesterday to ur question will help u,
Refer that .
KUMAR619 6-Feb-14 4:38am    
I tried But it not working for my code.
Copy my code and save as a XML file then run the code it wont work.

Please do that and after getting the solution
Post me the complete code.

Try it using xml file.

Waiting for your solution to this problem

here:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace Test_XmlChange
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Queue<XElement> duplicated = new Queue<XElement>();
                Queue<int> missing = new Queue<int>();

                XDocument d = XDocument.Load("XMLFile1.xml");
                var cs = d.Descendants("all").First().Descendants().OrderBy(i =>
                    int.Parse(i.Attribute("id").Value)).ToList();

                int lastId = -1;
                int id;

                for (int i = 0; i < cs.Count(); i++)
                {
                    // cs[i].Attributes("id").First().Value = (i+1).ToString();
                    id = int.Parse(cs[i].Attribute("id").Value);
                    if (id == lastId)
                    {
                        if (missing.Count > 0)
                            cs[i].Attribute("id").Value = missing.Dequeue().ToString();
                        else
                            duplicated.Enqueue(cs[i]);
                    }
                    if (id - lastId > 1)
                    {
                        for (int j = lastId + 1; j < id; j++)
                        {
                            if (duplicated.Count > 0)
                                duplicated.Dequeue().Attribute("id").Value = j.ToString();
                            else
                                missing.Enqueue(j);
                        }
                    }
                    lastId = id;
                }
                d.Save("XMLFile_out.xml");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}

/*
Filename: XMLFile1.xml

<?xml version="1.0" encoding="utf-8" ?>
<all>
  <component id="0"> KK</component>
  <component id="1"> KK</component>
  <component id="1"> MM</component>
  <component id="3"> QQ</component>
  <component id="4"> WW</component>
  <component id="4"> ZZ</component>
  <component id="6"> LL</component>
</all>

*/
 
Share this answer
 
v2
Comments
KUMAR619 4-Feb-14 6:05am    
I tried but its not my real xml My XML file has 12240 components.
I have changed some the elements
Example 1 for 19 , 4 for 2 and 1234 for 567

So if I execute I want to display the missing as well as repeated components with appropriate Id and also replace the repeated values with the values missing .


Is there any way to solve.



My Output before replacing



Starting Time: 2/4/2014 11:41:06 AM


XML READING


Component 0 is Repeated
Component 4 is Repeated
Component 8 is Repeated
Component 17 is Repeated
Component 42 is Repeated
Component 66 is Repeated
Component 1268 is Repeated
Component 10159 is Repeated
Component 10264 is Repeated
Component 11004 is Repeated



Component 2 is Missing
Component 7 is Missing
Component 16 is Missing
Component 19 is Missing
Component 44 is Missing
Component 65 is Missing
Component 1267 is Missing
Component 10158 is Missing
Component 10267 is Missing
Component 11005 is Missing
Component 11006 is Missing
Component 11008 is Missing

THE END

Ending Time: 2/4/2014 11:45:27 AM


Time Elapsed: 0 Hours 4 Minutes 21 Seconds
Vedat Ozan Oner 4-Feb-14 6:14am    
you have managed to detect which one is missing and which one is repeated. the only thing left is to set id as I did in the example. am I wrong?
KUMAR619 4-Feb-14 8:11am    
@Vedat Ozan Oner You are right but i've stored these values in list.
I have found the missing values using array and list so going back to the xml part with this list is tedious task. Is there any alternate way as you look at my code in the question and try your own code and send me the method which I can directly find the missing values and replace with the nearest possibility.

Waiting for you reply
Vedat Ozan Oner 5-Feb-14 11:10am    
I updated the solution.
KUMAR619 6-Feb-14 2:15am    
@Vedat Ozan Oner

I have updated my question please reply
Refer following code :-

C#
string szFilePath = @"D:\ABC.xml";//ur file path
           XmlDocument objDOM = new XmlDocument();
           objDOM.Load(szFilePath);//you can specify file name here
           int iIndex = 0;
           foreach (XmlNode objXNode in objDOM.GetElementsByTagName("Model")[0].ChildNodes)
           {
               if (objXNode.Name.Trim() == "Component")
               {
                   objXNode.Attributes["id"].Value = (iIndex).ToString();
                   iIndex++;
               }
           }
           objDOM.Save(szFilePath);
 
Share this answer
 
Comments
KUMAR619 6-Feb-14 5:59am    
@RhishikeshLathe You are a genius.
Its working for me.
I really had 12240 component Id's
All are working fine.

Thanks

Can you send me your facebook mailid. So that we can be friends
RhishikeshLathe 6-Feb-14 6:31am    
Thank you for ur comment.
You can do such xml manipulation, for that study XmlDocument class in C#.
I will give u my FB id,
So that we will be friendz..
KUMAR619 6-Feb-14 6:35am    
@RhishikeshLathe
Yes Sir.
What's your Facebook Id
RhishikeshLathe 6-Feb-14 6:55am    
Search by name, Rhishikesh Lathe,Pune.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900