如何从xelement列表中删除不需要的节点?



我需要使用c#在将服务的响应传递给调用者之前对其进行修剪。服务接受一系列XML格式的请求,格式化消息并将其发送到服务。响应看起来像这样:

<SvcRes>
<SvcVer>1.0</SvcVer>
<MsgUUID>12345678-1234-1234-1234-123456789012</MsgUUID>
<Svc>
<SvcParms>
<ApplID>App</ApplID>
<SvcID>AppSrch</SvcID>
<SvcVer>1.0</SvcVer>
</SvcParms>
<MsgData>
<AppResData>
<Key>xxxxxxxx</Key>
<Rslt>xxxxxxxx</Rslt>
<ErrCde>0</ErrCde>
<ApplMsgLst>
<ApplMsg>
<ApplMsgApplId>D6</ApplMsgApplId>
<ApplMsgNbr>0</ApplMsgNbr>
<ApplMsgTxt>INQUIRY COMPLETE     09:23:53</ApplMsgTxt>
<ApplMsgErrInd>N</ApplMsgErrInd>
</ApplMsg>
</ApplMsgLst>
</AppResData>
</MsgData>
<ErrCde>0</ErrCde>
<ErrMsg/>
</Svc>
<Svc>
<SvcParms>
<ApplID>DP</ApplID>
<SvcID>DPKywrd</SvcID>
<SvcVer>1.0</SvcVer>
<RqstUUID>12345678-1234-1234-1234-123456789012</RqstUUID>
</SvcParms>
<MsgData>
<AppResData>
<Key>xxxxxxxx</Key>
<Rslt>xxxxxxxx</Rslt>
<ErrCde>0</ErrCde>
<ApplMsgLst>
<ApplMsg>
<ApplMsgApplId>D6</ApplMsgApplId>
<ApplMsgNbr>0</ApplMsgNbr>
<ApplMsgTxt>INQUIRY COMPLETE     09:23:53</ApplMsgTxt>
<ApplMsgErrInd>N</ApplMsgErrInd>
</ApplMsg>
</ApplMsgLst>
</AppResData>
</MsgData>
<ErrCde>0</ErrCde>
<ErrMsg/>
<Svc>
<ErrCde>0</ErrCde>
<ErrMsg>Success</ErrMsg>
</SvcRes>

我需要拉出AppResData节点,然后在将结果发送回调用者之前从每个节点中剥离ApplMsgLst节点。生成的XML应该是这样的:

<AppResData>
<Key>xxxxxxxx</Key>
<Rslt>xxxxxxxx</Rslt>
<ErrCde>0</ErrCde>
</AppResData>
<AppResData>
<Key>xxxxxxxx</Key>
<Rslt>xxxxxxxx</Rslt>
<ErrCde>0</ErrCde>
</AppResData>
我可以使用以下两行代码获得XElement节点列表:
var xml = XElement.Parse(xmlResponse);
var msgData = xml.DescendantsAndSelf("MsgData");
  • 然后我可以做string. concat (msgData.Nodes())来获得最后的字符串返回给调用者——但在这一点上,我不知道如何删除内部ApplMsgLst节点。我已经尝试将其转换回字符串并重新解析,但当然它会抱怨多个根元素。我已经尝试了我能想到的所有删除组合,但它们总是删除太多(所有内容)或没有。

还有别的方法吗?没有文件,这是SOAP web服务中的所有字符串数据。

这是一个基于XSLT的解决方案。

参数:

  • 以字符串数据类型输入XML。
  • XSLT文件。
  • 以字符串数据类型输出XML。

XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/SvcRes">
<root>
<xsl:for-each select="Svc/MsgData/AppResData">
<xsl:copy>
<xsl:copy-of select="Key"/>
<xsl:copy-of select="Rslt"/>
<xsl:copy-of select="ErrCde"/>
</xsl:copy>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>

c#

void Main()
{
const string XSLTFILE = @"e:TempDaveN59.xslt";
string inputXML = @"<SvcRes>
...
</SvcRes>";
try
{
StringReader rdr = new StringReader(inputXML);
XPathDocument XPathDoc = new XPathDocument(rdr);
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(XSLTFILE);
StringWriter sw = new StringWriter();
using (XmlWriter xwo = XmlWriter.Create(sw, xslt.OutputSettings))
{
xslt.Transform(XPathDoc, xwo);
}

Console.WriteLine(sw.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

输出XML

<root>
<AppResData>
<Key>xxxxxxxx</Key>
<Rslt>xxxxxxxx</Rslt>
<ErrCde>0</ErrCde>
</AppResData>
<AppResData>
<Key>xxxxxxxx</Key>
<Rslt>xxxxxxxx</Rslt>
<ErrCde>0</ErrCde>
</AppResData>
</root>

您可以尝试使用Cinchoo ETL -一个开源库来轻松完成此转换

StringBuilder xml = new StringBuilder();
using (var r = ChoXmlReader.LoadText("*** YOUR XML CONTENTS ***")
.WithXPath("//AppResData")
.WithField("Key")
.WithField("Rslt")
.WithField("ErrCde")
)
{
using (var w = new ChoXmlWriter(xml)
)
{
w.Write(r);
}
}
Console.WriteLine(xml.ToString());

输出:

<Root>
<AppResData>
<Key>xxxxxxxx</Key>
<Rslt>xxxxxxxx</Rslt>
<ErrCde>0</ErrCde>
</AppResData>
<AppResData>
<Key>xxxxxxxx</Key>
<Rslt>xxxxxxxx</Rslt>
<ErrCde>0</ErrCde>
</AppResData>
</Root>

最新更新