简体   繁体   English

使用 Linq 获取 CDATA XML 部分

[英]Getting CDATA XML Section Using Linq

I have searched around looking for ways to get at the CDATA section text area and found very luk warm solutions using linq.我四处搜索,寻找进入 CDATA 部分文本区域的方法,并使用 linq 找到了非常温暖的解决方案。 I need to extract the XML imbedded in the CDATA Section so I can pull out different pieces of info.我需要提取嵌入在 CDATA 部分中的 XML,以便我可以提取不同的信息。 I have the following XML:我有以下 XML:

<Envelope>
 <Warehouse />
 <Payload>
  - <![CDATA[ 
    <?xml version="1.0"?>
    <ROOT>
      <ORDERID>399798</ORDERID>
      <PRODUCTNUMBER>00003997</PRODUCTNUMBER>
      <DESCIPTION>Red Rider BB-Gun</DESCIPTION>
      <STATUS>InStock</STATUS>
      <LOCATION>Chicago</LOCATION>
    </ROOT> ]]>
 </Payload>
</Envelope>

So I would like if possible to do this by extracting the whole cdata section into an XDocument so I can use Linq to query.因此,如果可能的话,我希望通过将整个 cdata 部分提取到 XDocument 中来做到这一点,以便我可以使用 Linq 进行查询。 Also if I just wanted to pull out a single Element from the CData section.另外,如果我只想从 CData 部分中提取一个元素。 How can I do this?我怎样才能做到这一点? Using Linq?使用林克?

I have tried using this Linq code below to give me back cdata section but can't do anything with it since it comes back as an IEnumerable.我曾尝试使用下面的这个 Linq 代码来返回 cdata 部分,但由于它作为 IEnumerable 返回,因此无法对它做任何事情。 I'm probably missing something easy so I come to you the Linq wizards for some help.我可能遗漏了一些简单的东西,所以我来找你的 Linq 向导寻求帮助。

Here's the code I mentioned:这是我提到的代码:

 var queryCDATAXML = from el in xdoc.DescendantNodes()
                     where el.NodeType == XmlNodeType.CDATA
                     select el.Parent.Value.Trim();

Is there a way to do a select new XDocument or XmlDocument like so:有没有办法像这样选择新的 XDocument 或 XmlDocument:

//This doesn't compile.
 var queryCDATAXML = from el in xdoc.DescendantNodes()
                            where el.NodeType == XmlNodeType.CDATA
                            select new XDocument()
                              {  
                                 el.Parent.Value.Trim();
                              }   

If I am going about this all wrong or their is a better way to accomplish this I'm open for suggestions.如果我对这一切都做错了,或者他们是实现这一目标的更好方法,我愿意接受建议。 :) :)

Thanks, DND谢谢,免打扰

Code Update:代码更新:

var queryCDATAXML = from el in xdoc.DescendantNodes()
                     where el.NodeType == XmlNodeType.CDATA
                     select el.Parent.Value.Trim();

var xdoc = XDocument.Parse(queryCDATAXML);

Generates this error: Argument '1': cannot convert from 'System.Collections.Generic.IEnumerable' to 'string'.生成此错误:参数“1”:无法从“System.Collections.Generic.IEnumerable”转换为“string”。

Rather than new XDocument , try XDocument.Parse而不是new XDocument ,试试XDocument.Parse

var queryCDATAXML = // get the value
var xdoc = XDocument.Parse(queryCDATAXML);

You're getting some Enumerable<string> rather than string , so you need to just get the single value.您将获得一些Enumerable<string>而不是string ,因此您只需要获得单个值。

var node = xdoc.DescendantNodes().Single(el => el.NodeType == XmlNodeType.CDATA);
var content = node.Parent.Value.Trim();
var xdoc = XDocument.Parse(content);

I resolved this case in this form:我以这种形式解决了这个案例:

XDocument xdoc = XDocument.Parse(vm.Xml);

XNamespace cbc = @"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2";
  var list2 =
       (from el in xdoc.Descendants(cbc + "Description")
        select el).FirstOrDefault();

      var queryCDATAXML = (from eel in list2.DescendantNodes()                                                
      select eel.Parent.Value.Trim()).FirstOrDefault();

I hope help them我希望能帮助他们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM