我有Xml在下面的格式
<?xml version="1.0" encoding="UTF-8"?>
<htmlString>
<ACTION v="Search"/>
<KEY/>
<ACCOUNTNO v=""/>
<AGREEMENTREF1/>
<AGREEMENTREF2/>
<PAYMENTREF1/>
<PAYMENTREF2/>
<PROGRAM v="SOW03301"/>
<SESSION v="SF3gGV1J4501"/>
<OBJECT v="1"/>
<HTMLFILE v="CGIOTP000023/TEMP:WHTTP"/>
<PIPECOUNTER v="100000386"/>
</htmlString>
我需要将ACTION和SESSION之间的部分作为字符串。
<KEY/>
<ACCOUNTNO v=""/>
<AGREEMENTREF1/>
<AGREEMENTREF2/>
<PAYMENTREF1/>
<PAYMENTREF2/>
<PROGRAM v="SOW03301"/>
你能帮忙吗。谢谢。
答案 0 :(得分:0)
您可以使用XElement.ElementsAfterSelf
:
string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<htmlString>
<ACTION v=""Search""/>
<KEY/>
<ACCOUNTNO v=""""/>
<AGREEMENTREF1/>
<AGREEMENTREF2/>
<PAYMENTREF1/>
<PAYMENTREF2/>
<PROGRAM v=""SOW03301""/>
<SESSION v=""SF3gGV1J4501""/>
<OBJECT v=""1""/>
<HTMLFILE v=""CGIOTP000023/TEMP:WHTTP""/>
<PIPECOUNTER v=""100000386""/>
</htmlString>";
var xd = XDocument.Parse(xml);
var elements = xd.Root.Element("ACTION")
.ElementsAfterSelf()
.TakeWhile(xe => xe.Name.LocalName != "SESSION")
.Select(xe => xe.ToString());
var sbNew = string.Join(Environment.NewLine, elements);
Console.Write(sbNew);
Console.ReadLine();
打印:
<KEY />
<ACCOUNTNO v="" />
<AGREEMENTREF1 />
<AGREEMENTREF2 />
<PAYMENTREF1 />
<PAYMENTREF2 />
<PROGRAM v="SOW03301" />