Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
How to find missing values in XML file using C#

XML
<Model>
<Component Id="1"><Color></Color>
<Component Id="2"><Color></Color>
<Component Id="2"><Color></Color>
<Component Id="4"><Color></Color>
<Component Id="5"><Color></Color>
<Component Id="6"><Color></Color>
<Component Id="6"><Color></Color>
<Component Id="8"><Color></Color>
<Component Id="9"><Color></Color>
</Model>



IN the above XML code Id="2" and Id="6" is repeated.
Do I want to find out the missing values from sequence.
I have to replace the repeated values with nearest possible values

Example:

The Output should be:

XML
<Model>
<Component Id="1"><Color></Color>
<Component Id="2"><Color></Color>
<Component Id="3"><Color></Color>
<Component Id="4"><Color></Color>
<Component Id="5"><Color></Color>
<Component Id="6"><Color></Color>
<Component Id="7"><Color></Color>
<Component Id="8"><Color></Color>
<Component Id="9"><Color></Color>
</Model>


I there any way to solve this problem using C#
Posted
Comments
RhishikeshLathe 5-Feb-14 1:39am    
xml provided by u is not in proper format,
Component tags are not closed

1 solution

Please refer following code, i have added closing tags for Component tags:-

XML
string szXML = "<Model>" +
                           "<Component Id='1'><Color></Color></Component>" +
                           "<Component Id='2'><Color></Color></Component>" +
                           "<Component Id='2'><Color></Color></Component>" +
                           "<Component Id='4'><Color></Color></Component>" +
                           "<Component Id='5'><Color></Color></Component>" +
                           "<Component Id='6'><Color></Color></Component>" +
                           "<Component Id='6'><Color></Color></Component>" +
                           "<Component Id='8'><Color></Color></Component>" +
                           "<Component Id='9'><Color></Color></Component>" +
                        "</Model>";

           XmlDocument objDOM = new XmlDocument();
           objDOM.LoadXml(szXML);//you can specify file name here
           for (int iIndex = 0; iIndex < objDOM.DocumentElement.ChildNodes.Count; iIndex++)
           {
               objDOM.DocumentElement.ChildNodes[iIndex].Attributes["Id"].Value = (iIndex + 1).ToString();
           }
           //objDOM.Save(szFileName) //if xml loaded from File
 
Share this answer
 
Comments
KUMAR619 5-Feb-14 2:53am    
XmlDocument objDOM = new XmlDocument();
objDOM.LoadXml("7049_PIPING - Copy.xml");


for (int iIndex = 0; iIndex < objDOM.DocumentElement.ChildNodes.Count; iIndex++)
{
objDOM.DocumentElement.ChildNodes[iIndex].Attributes["Id"].Value = (iIndex + 1).ToString();
}
objDOM.Save("7049_PIPING - Copy.xml");


ERROR:Data at the root level is invalid. Line 1, position 1.

What should I do
RhishikeshLathe 5-Feb-14 4:06am    
Use
objDOM.Load(szXmlFileFullPath);//ur full path of xml file
instead of
objDOM.LoadXml("7049_PIPING - Copy.xml");
KUMAR619 5-Feb-14 4:53am    
Thanks your Code in string file is working fine.
But I dont know how to add path.
Can you help me:

Its path:

C:\Users\Kumar\7049_PIPING - Copy.xml


Can you add this path in the above code .

Waiting for your code.

Thanks
RhishikeshLathe 5-Feb-14 7:31am    
string szFilePath = @"C:\Users\Kumar\7049_PIPING - Copy.xml";//ur file path
XmlDocument objDOM = new XmlDocument();
objDOM.Load(szFilePath);//you can specify file name here
for (int iIndex = 0; iIndex < objDOM.DocumentElement.ChildNodes.Count; iIndex++)
{
objDOM.DocumentElement.ChildNodes[iIndex].Attributes["Id"].Value = (iIndex + 1).ToString();
}
objDOM.Save(szFilePath);

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