正在c#中获取具有指定标题的XElement
本文关键字:标题 XElement 获取 正在 | 更新日期: 2023-09-27 18:22:14
这是xml文档,我需要从c#中的servicelist中获得一个具有指定标题的服务。假设我有一个标题为"Collect Call and SMS"的服务,如下所示。我想获得这个服务,这样我就可以从XDocument中删除它。注意,我从数据库中以的形式获得这个xml字符串
C#
XDocument-doc=XDocument.Parse(xmlstring);
XML文档
<?xml version="1.0" encoding="utf-16" ?>
- <ServiceList>
- <Service>
<COMPAT>2</COMPAT>
<EQUIPID>0</EQUIPID>
<TITLE>Collect Call and SMS</TITLE>
<SMSCOMMAND>0</SMSCOMMAND>
<DIALCOMMAND>123</DIALCOMMAND>
<DEACTIVATIONCOMMAND>0</DEACTIVATIONCOMMAND>
<MODE>Dial</MODE>
<DETAIL>Here you go</DETAIL>
<IMAGE>2014-18-9--11-47-33</IMAGE>
- <LONGDESC>
- <![CDATA[ <p>Service</p>
<p>Service</p>
]]>
</LONGDESC>
- <Mechanism>
<Title>Mech Title</Title>
<Description />
<Trigger>Mech Trigger</Trigger>
<Controls>1</Controls>
</Mechanism>
</Service>
- <Service>
<COMPAT>2</COMPAT>
<EQUIPID>0</EQUIPID>
<TITLE>MS Call and SMS</TITLE>
<SMSCOMMAND>SUB,6104</SMSCOMMAND>
<DIALCOMMAND>0</DIALCOMMAND>
<DEACTIVATIONCOMMAND>UNSUB,6104</DEACTIVATIONCOMMAND>
<MODE>SMS</MODE>
<DETAIL>Service</DETAIL>
<IMAGE>2014-18-9--11-47-59</IMAGE>
- <LONGDESC>
- <![CDATA[ <p>P<br />P<br />P </p>
]]>
</LONGDESC>
- <Mechanism>
<Title>Mech Title</Title>
<Description>Service</Description>
<Trigger>Mech Trigger</Trigger>
<Controls>1</Controls>
</Mechanism>
</Service>
</ServiceList>
var xd = XDocument.Parse(xml);
xd.Root.Elements("Service") // Enumerate the service elements
.Where
(
x=>
(string)x.Element("TITLE") == "Collect Call and SMS"
) // Find the ones you are interested in
.Remove(); //Remove them from the document