How do I use the data values from an xml and replace the data values of another xml using c#

I have a  reasonable understanding of using c# but I am actually a SAS Developer. I require the use of c# and xml due to the limitations of SAS to output a structured xml file.

Essentially what I am trying to do is keep a template xml for each table I need to produce. My system will then output an xml which will have the data in the correct order but will not have the formatting as in the template. For example blank lines between values as would be the case in the template.

I want to take the first data value from the output and use that to replace the first data value in the template and then the 2nd from the data and update the 2nd in the template and so on. I would like this to then be written to a new xml leaving the template in tact for future use.

If you require any further information from me to make this easier please let me know.
marco071215Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gery128Commented:
You will need to first read XML and in the same loop/function start writing your new XML as per new template.

You can refer this link:
Reading and Writing XML in C#
0
marco071215Author Commented:
Thank you for the comment and the link. However, I am not sure it entirely gives me what I need, or if the information is all there I am unsure how best to utilise it.

I have attached the way my code currently looks which may be beneficial. If you think that I am heading the wrong direction could you please let me know and maybe point out the section you are referring to on the link.

{
	XmlTextReader myReader = new XmlTextReader("C:\\marco\\marco_test1_table.xml");
	XmlDocument mySourceDoc = new XmlDocument();
	mySourceDoc.Load(myReader);
	myReader.Close(); 
 
	XmlTextReader myTemplateReader = new XmlTextReader("C:\\marco\\template.xml");
	XmlDocument myTemplateDoc = new XmlDocument();
	myTemplateDoc.Load(myTemplateReader);
	myTemplateReader.Close(); 
          	
	XmlTextWriter myWriter = new XmlTextWriter ("C:\\marco\\Marcos_Out_File.xml", null);
	myWriter.Formatting = Formatting.Indented;

	XmlNodeList dataList = mySourceDoc.GetElementsByTagName("Data");
	XmlNodeList templateList = myTemplateDoc.GetElementsByTagName("Data");

			
	IEnumerator enumerator = dataList.GetEnumerator();

	foreach(XmlNode srcNode in templateList)
		{			
			if (srcNode.Attributes.GetNamedItem("ss:Type").Value == "Number")
			{
				srcNode.Value = enumerator.Current.ToString();
			}
				enumerator.MoveNext();
		}
	

	myTemplateDoc.WriteTo(myWriter);
	myWriter.Close();
}

Open in new window

0
gery128Commented:
It seems that you are heading in correct direction...
If you are stuck or get any problem, then you can always post here...
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

marco071215Author Commented:
To be honest that is the point I am at now.

There are two main issues.

The builder is telling me that the "enumerator.MoveNext();" line is incorrect and also when I remove the MoveNext line it says that the line "srcNode.Value = enumerator.Current.ToString();" preceeding this is also incorrect. Do these seem logical to you?

If you could help I really would appreciate it.
0
gery128Commented:
@marco

What exact error its throwing it on enumerator.MoveNext() line? What exactly 'builder' means? are you working in Visual Studio ?
I compiled above code in visual studio without any problem, however to run and test it, I will require all those XML files (samples)
0
marco071215Author Commented:
@gery128

I am using visual studio. At compile time it runs ok. But as soon as I do a live debug run it falls over.

I have attached the test files so that you will be able to try it out for yourself and hopefully crack the problem for me.

Thank you for all of your help so far.
marco-test1-table.xml
template.xml
0
gery128Commented:
Your code complied fine on my machine. check if you are importing System.Collections namespace at the top.
Also, I just wanted to know, what you wanted to change in new XML if ss:Type=Number in old XML?

You can't directly get text of node using this line:
srcNode.Value = enumerator.Current.ToString();

I replaced above line with these lines:
if (srcNode.Attributes.GetNamedItem("ss:Type").Value == "Number")
                {
                    XmlElement element = (XmlElement)enumerator.Current;
                    srcNode.InnerText = element.InnerText;
                }

Let me know your exact functional requirement.
0
marco071215Author Commented:
Sorry for the delay in getting back to you. I had to wait until I was back in the office to test.

I did have System.Collections namespace at the top and when I made the changes you pointed out it ran through, which is more than it has done so far.

The output file was produced but the data values in the template were replaced with the string values from the data file.

The requirement I am looking for is that the data value in the template is replaced with the data value from the initial file (marco_test1_table).

Could you let me know what I have to do to change that?

0
gery128Commented:
Okay, so you want the corresponding integer values to be written. For that, we just need to put that enumerator.MoveNext() before the checking for ss:type=Number, I have modified code and pasting here updated one.

Also, I have removed xmlTextReader for both template and data files, as you can directly load the xml document in XmlDocument class by just passing file URL of the file: find complete code below.
XmlDocument mySourceDoc = new XmlDocument();
            mySourceDoc.Load("D:\\NEW_DL\\Development\\Samples\\4-TierArchitectureNew\\images\\marco_test1_table.xml");

            XmlDocument myTemplateDoc = new XmlDocument();
            myTemplateDoc.Load("D:\\NEW_DL\\Development\\Samples\\4-TierArchitectureNew\\images\\template.xml");

            XmlTextWriter myWriter = new XmlTextWriter("D:\\NEW_DL\\Development\\Samples\\4-TierArchitectureNew\\images\\Marcos_Out_File.xml", null);
            myWriter.Formatting = Formatting.Indented;

            XmlNodeList dataList = mySourceDoc.GetElementsByTagName("Data");
            XmlNodeList templateList = myTemplateDoc.GetElementsByTagName("Data");


            IEnumerator enumerator = dataList.GetEnumerator();

            foreach (XmlNode srcNode in templateList)
            {
                enumerator.MoveNext();
                if (srcNode.Attributes.GetNamedItem("ss:Type").Value == "Number")
                {
                    XmlElement element = (XmlElement)enumerator.Current;
                    srcNode.InnerText = element.InnerText;
                }               
            }
            myTemplateDoc.WriteTo(myWriter);
            myWriter.Close();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
marco071215Author Commented:
Thank you for all of your help, it is greatly appreciated.
0
gery128Commented:
You are welcome :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.