• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

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.
0
marco071215
Asked:
marco071215
  • 6
  • 5
1 Solution
 
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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
 
marco071215Author Commented:
Thank you for all of your help, it is greatly appreciated.
0
 
gery128Commented:
You are welcome :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now