Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one requirement of parsing an existing .xml file.
I need to read the existing .xml file, parse some tags inside it and dump the same to a new .xml file.The original .xml files are generated as and when required basis inside a directory.I need to check if a new files comes inside the directory if it comes (old files will be there in side the directory and they will not be purged), then parse the .xml file and generate the new one.

As a sample I have attached the original .xml file in
Original_xmldirectory
and the converted one into
Converted_Xmldirectory
inside the attached folder.

Please note that the equipment number information is not present in the above .xml file.
This needs to be fetched from Matching equipment no. from an .xlsx file (where equipment no field is mapped with SSNAME.device.devicename.pointtype column in .xlsx file as attached).

The we need to read the existing .xml file and read "SSNAME.device.devicename.pointtype" information from
<triggerdata>High Warning.abcd.Region.xy2.SSNAME.device.devicename.pointtype=40.00 status=real tag,
search in the equipment_map.xlsx sheet and find the equipment no. from the same.
This equipment no. needs to be tagged under <equipment_number>1234567890.
The <state> tag to be removed from the converted .xml file.
Now we need to separate the following information from,

<triggerdata>High Warning.abcd.Region.xy2.SSNAME.device.devicename.pointtype=40.00 status=real
and segregate the information as below.

<Substation>SSNAME</Substation>
<DeviceType>device</DeviceType>
<Device>devicename</Device>.


Finally the information is to be dumped to a new .xml file in the following location.
c:\converted_xml\ .

So lastly the new .xml file will look as follows.

<Event>
<Equipment_Number>1234567890</Equipment_Number>
<AlarmText>High Warning</Alarmtext>
<StartDate>5/26/2017 5:10:00 PM</StartDate>
<EndDate>5/26/2017 5:14:30 PM</EndDate>
<Substation>SSNAME</Substation>
<DeviceType>device</DeviceType>
<Device>devicename</Device>.
</Event>


Can anyone help me out in this regard?

The Original xml file is as follows.
<?xml version="1.0"?>

-<Email>

<To>abcd@pqrst.com</To>

<Cc/>

<Bcc/>

<Subject>HighAlarm</Subject>

<Message>HighAlarmtarun</Message>

<Event>

<State>High Warning</State>

<StartDate>5/26/2017 5:10:00 PM</StartDate>

<EndDate>5/26/2017 5:14:30 PM</EndDate>

<TriggerData>High Warning.abcd.Region.xy2.SSNAME1.device1.devicename1.pointtype1=40.00 status=real </TriggerData>

</Event>

The above file needs to be converted to,
<Event>
<Equipment_Number>1234567890</Equipment_Number>
<AlarmText>High Warning</Alarmtext>
<StartDate>5/26/2017 5:10:00 PM</StartDate>
<EndDate>5/26/2017 5:14:30 PM</EndDate>
<Substation>SSNAME1</Substation>
<DeviceType>device1</DeviceType>
<Device>devicename1</Device>.
</Event>


----------------------------------------------
The
equipment_map.xlsx
file contains the following details.

SL. No	Equipment Name	Equipment ID
		
1	SSNAME1.device1.devicename1.pointtype1	1234567890
2	SSNAME2.device2.devicename2.pointtype2	11213141516
3	SSNAME3.device3.devicename3.pointtype3	17181920212223

Another .xml file contains the following information as generated and the third and so on.
--------------------------------------------------------
2nd one,
<?xml version="1.0"?>

-<Email>

<To>abcd@pqrst.com</To>

<Cc/>

<Bcc/>

<Subject>HighAlarm</Subject>

<Message>HighAlarmtarun</Message>

<Event>

<State>High Warning</State>

<StartDate>5/26/2017 5:20:00 PM</StartDate>

<EndDate>5/26/2017 5:25:30 PM</EndDate>

<TriggerData>High Warning.abcd.Region.xy2.SSNAME2.device2.devicename2.pointtype2=40.00 status=Good </TriggerData>

</Event>


the third one .............................
-----------------------------------------------------------------------------
I can not attached the directory.But the directory fills up with new xml files when they are auto generated by some application as and when some event occurs in the system.<event> tag depicts the event.

What I have tried:

Tried to read a file using the xmlreader class.
Posted
Updated 29-May-17 3:50am
v2
Comments
[no name] 29-May-17 9:20am    
"Can anyone help me out in this regard", since we do not have your XML files, your Excel files, your code or anything else, what is it that you would expect from us in the way of help specific to the code you have written?
Member 12708425 29-May-17 9:44am    
Sorry.I was in search of attaching the files with directory structure.But not able to do so.I have then copy the files in the question itself.
[no name] 29-May-17 9:47am    
And? There isn't a single line of C# code in here that you have tried to do your job yourself before trying to get people here to do it for you.
Member 12708425 21-Jul-17 13:33pm    
Code tried.
-----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("C:\\Users\\xxxx\\Desktop\\xml file\\xyz_present.xml");
// loop through each file


// cycle through each child noed
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
// first node is the url ... have to go to nexted loc node
foreach (XmlNode locNode in node)
{
// thereare a couple child nodes here so only take data from node named loc
if (locNode.Name == "TriggerData")
{
// get the content of the loc node
string loc = locNode.InnerText;
string[] words = loc.Split('.');
foreach(String s in words)
{
Console.WriteLine(s + Environment.NewLine);
}
// write it to the console so you can see its working
Console.WriteLine(loc + Environment.NewLine);

}
}
}
Console.ReadLine();
}
}
}
But this is just to split the line into strings.How to separately attribute the tag and create a <root> tag?
Member 12708425 26-Jul-17 13:15pm    
I have uploaded so portion of the code That I have tried. Can you have a look on the same?

1 solution

Use XmlDocument[^] with XPath[^] or XSLT[^]...
 
Share this answer
 

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