使用核心服务从组件检索列表项值


1

如何使用核心服务从组件检索列表项值。下面是xml。

<FaqCategory xmlns="http://www.xyz.com/tridion/schemas"> 
    <Title>Denied boarding compensation options</Title> 
    <url>Denied boarding compensation options</url> 
    <List xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="tcm:229-2468565" xlink:title="120. When will I get my voucher and where do I collect it from?"></List> 
    <List xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="tcm:229-2468581" xlink:title="210. Can I upgrade my compensation ticket using my Skywards Miles?"></List> 
    <List xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="tcm:229-2468568" xlink:title="140. Can I give the voucher to family or friends?"></List> 
</FaqCategory> 

在一个字符串生成器,我想输出是:

“?120.我什么时候可以拿到我的优惠券,并在那里我收集它从”,“210可以升级我使用我的Skywards里程奖励机票?“。

我试过使用正则表达式,但没有用。请建议。

1

使用RegEx解析XML可能会让你疯狂。

下面就来分析XML有点冲的方式...

string xml = "<FaqCategory xmlns=\"http://www.xyz.com/tridion/schemas\"><Title>Denied boarding compensation options</Title><url>Denied boarding compensation options</url><List xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:type=\"simple\" xlink:href=\"tcm:229-2468565\" xlink:title=\"120. When will I get my voucher and where do I collect it from?\"></List><List xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:type=\"simple\" xlink:href=\"tcm:229-2468581\" xlink:title=\"210. Can I upgrade my compensation ticket using my Skywards Miles?\"></List><List xmlns:xlink=\"http://www.w3.org/1999/xlink\" xlink:type=\"simple\" xlink:href=\"tcm:229-2468568\" xlink:title=\"140. Can I give the voucher to family or friends?\"></List></FaqCategory>"; 
XDocument xdoc = XDocument.Parse(xml); 
string faqNs = "http://www.xyz.com/tridion/schemas"; 
string xlinkNs = "http://www.w3.org/1999/xlink"; 

StringBuilder s = new StringBuilder(); 

XElement rootNode = xdoc.Element(XName.Get("FaqCategory", faqNs)); 

s.Append("Title: "); 
s.Append(rootNode.Element(XName.Get("Title", faqNs)).Value); 
s.Append(Environment.NewLine); 
s.Append("Url: "); 
s.Append(rootNode.Element(XName.Get("url", faqNs)).Value); 
s.Append(Environment.NewLine); 
foreach (XElement node in rootNode.Elements(XName.Get("List", faqNs))) 
{ 
    s.Append(node.Attribute(XName.Get("title", xlinkNs)).Value); 
    s.Append(Environment.NewLine); 
} 

Console.WriteLine(s.ToString());