通过C#解析.xml文件 [英] Parsing .xml file through C#

查看:88
本文介绍了通过C#解析.xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个解析现有.xml文件的要求。

我需要读取现有的.xml文件,解析其中的一些标签并将其转储到新的.xml文件中。原始的.xml文件是在目录内部生成的。当需要时,我需要检查目录中是否有新文件(旧文件将存在于目录中并且它们不会被清除),然后解析.xml文件并生成新文件。



作为示例,我已将原始.xml文件附加到

 Original_xmldirectory 

并将转换后的一个转换为附加文件夹中的

 Converted_Xmldirectory 





请注意设备编号信息是不存在于上面的.xml文件中。

这需要从匹配设备中获取。来自.xlsx文件(其中设备没有字段与.xlsx文件中的SSNAME.device.devicename.pointtype列一起映射)。



我们需要阅读现有的.xml文件,并从

< triggerdata>中读取SSNAME.device.devicename.pointtype信息High Warning.abcd.Region.xy2.SSNAME.device.devicename.pointtype = 40.00 status =真实标签,

在equipment_map.xlsx表中搜索并找到没有的设备。来自同一个。

此设备没有。需要在< equipment_number> 1234567890下标记。

< state>要从转换后的.xml文件中删除标记。

现在我们需要将以下信息分开,



< triggerdata>高Warning.abcd.Region.xy2.SSNAME.device.devicename.pointtype = 40.00 status = real

并将信息分隔如下。



< Substation> SSNAME< / Substation> 
< DeviceType>设备< / DeviceType>
< Device> devicename< / Device>。





最后将信息转储到新的.xml文件在以下位置。

c:\converted_xml \。



最后,新的.xml文件如下所示。 />


<事件> 
< Equipment_Number> 1234567890< / Equipment_Number>
< AlarmText>高警告< / Alarmtext>
< StartDate> 5/26/2017 5:10:00 PM< / StartDate>
< EndDate> 5/26/2017 5:14:30 PM< / EndDate>
< Substation> SSNAME< / Substation>
< DeviceType>设备< / DeviceType>
< Device> devicename< / Device>。
< / Event>





有谁可以帮我解决这个问题?



原始xml文件如下。

<?xml version =1.0?> 

- <电子邮件>

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

< Cc />

< Bcc />

< Subject> HighAlarm< / Subject>

<消息> HighAlarmtarun< /消息>

<事件>

<状态>高警告< /状态>

< 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>



上述文件需要转换为,

<事件> 
< Equipment_Number> 1234567890< / Equipment_Number>
< AlarmText>高警告< / 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>





------------------ ----------------------------

 equipment_map.xlsx 

文件包含以下详细信息。



 SL。否设备名称设备ID 

1 SSNAME1.device1.devicename1.pointtype1 1234567890
2 SSNAME2.device2.devicename2.pointtype2 11213141516
3 SSNAME3.device3.devicename3.pointtype3 17181920212223



另一个.xml文件包含生成的以下信息和第三个等等。

------------ --------------------------------------------

第二个,

<?xml version =1.0?> 

- <电子邮件>

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

< Cc />

< Bcc />

< Subject> HighAlarm< / Subject>

<消息> HighAlarmtarun< /消息>

<事件>

<状态>高警告< /状态>

< 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>





第三个........... ..................

------------------------ -------------------------------------------------- ---

我无法附加目录。但当某个应用程序自动生成新的xml文件时,当系统中发生某些事件时,该目录会填满新的xml文件。< event>标签描绘了这个事件。



我尝试了什么:



试过使用xmlreader类读取文件。

解决方案

使用 XmlDocument [ ^ ] XPath [ ^ ]或 XSLT [ ^ ] ...

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.

解决方案

Use XmlDocument[^] with XPath[^] or XSLT[^]...


这篇关于通过C#解析.xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆