Link to home
Start Free TrialLog in
Avatar of marco071215
marco071215

asked on

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.
Avatar of gery128
gery128
Flag of India image

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#
Avatar of marco071215
marco071215

ASKER

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

It seems that you are heading in correct direction...
If you are stuck or get any problem, then you can always post here...
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.
@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)
@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
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.
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?

ASKER CERTIFIED SOLUTION
Avatar of gery128
gery128
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for all of your help, it is greatly appreciated.
You are welcome :)