开发者

LINQ To XML Getting a value without the nodes inside

开发者 https://www.devze.com 2023-03-14 04:18 出处:网络
I have this XML: <chunk type=\"manufacturer_info\" id=\"\" note=\"\">test: <chunk type=\"style\" style=\"link\">${manufacturer_website}</chunk></chunk>

I have this XML:

<chunk type="manufacturer_info" id="" note="">test: <chunk type="style" style="link">${manufacturer_website}</chunk></chunk>

I need to get "test: " 开发者_如何学编程separately from the inner element.

EDIT: This is coming into a function as an XElement.


The <chunk> element has two child nodes: a text node and a <chunk> element.

You can get the value of the text node as follows:

var element = XElement.Parse(@"<chunk type=""manufacturer_info"" ...");

var result = string.Concat(element.Nodes().OfType<XText>());
// result == "test: "


Here you go.

          string xml =  @"<Chunks><chunk type='manufacturer_info' id='' note=''>test:                <chunk type='style' style='link'>${manufacturer_website}</chunk></chunk></Chunks>";

          var xDoc = XDocument.Parse(xml);
          var res = xDoc.DescendantNodes().OfType<XText>().First().Value;
0

精彩评论

暂无评论...
验证码 换一张
取 消