【问题标题】:How to convert IEnumerable<XElement> to string如何将 IEnumerable<XElement> 转换为字符串
【发布时间】:2016-03-28 10:45:54
【问题描述】:

我有一个字符串,其中包含一些我想要操作的数据,所以我将它发送到执行此操作的函数。

我想在他对函数进行操作后返回那个字符串。

我要做的是在他被操纵后从包含正确内容的函数中返回IEnumerable&lt;XElement&gt;

我试图从函数中返回xmlElements.ToString();,但它包含一些奇怪的字符串,而不是它本身的实际字符串。

renderedOutput= XML 结构中的字符串

var xml = XElement.Parse(renderedOutput);
IEnumerable<XElement> xmlElements = xml.Descendants();

然后我将它发送到那个函数(改变一些值)

 xmlElements = SetAdditionalData(xmlElements, docIDs[i], i, orderID);// This is the place where i want to get a String in XML strcture

这只是函数作用的一个例子:

public IEnumerable<XElement> SetAdditionalData(IEnumerable<XElement> xmlElements, string docID, int i, string orderID)
{
    foreach (var el in xmlElements)
    {
        // set country name from country iso
        if (el.Name == "Country")
        {                                      
            var isoC = el.Value.ToString().Trim();
            if (isoC != string.Empty)
            {
                RegionInfo isoCode = new RegionInfo(isoC);
                el.SetValue(isoCode.DisplayName);
            }
            continue;                    
        }
    }

    return xmlElements;  // tried to return xmlElements.ToSting(), but it didn't returned the right data
}

我正在努力实现这一目标:

xmlElements 将包含操作后的字符串 - 意思是在SetAdditionalData() 设置一些附加数据之后

【问题讨论】:

  • @Henk 我期待XML string
  • @Henk,问我提到过-我正在寻找不起作用的xmlElements.ToSting()

标签: c# asp.net xml-parsing


【解决方案1】:

也许

return String.Concat(xmlElements.Select(element => element.OuterXml));

为了它的价值,我只返回元素 - 这样函数只做一件事,修改元素。然后,如果调用者需要一个字符串,它可以将元素转换为字符串。

【讨论】:

  • 我收到此错误:'System.Collections.Generic.IEnumerable&lt;System.Xml.Linq.XElement&gt;' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Collections.Generic.IEnumerable&lt;System.Xml.Linq.XElement&gt;' could be found (are you missing a using directive or an assembly reference?)
  • XElement 上没有属性 OuterXml。要从 IEnumerable 获取 XML,只需调用如下代码:var xml = string.Concat(xmlElements)var xml = string.Join("\r\n", xmlElements)
  • XElement inherits OuterXml 来自XmlNode
  • 这对我有用,返回 String.Concat(xmlElements.Select(element => element.ToString()));
  • XmlElement 继承自 XmlNode,而 XElement 继承自 XNode(@chviLadislav 指出它没有 OuterXml 属性)
【解决方案2】:

尝试以下选项: 如果内容是文本,则获取元素的内容:

xmlElements.Value

以 XML 格式获取元素的内容:

xmlElements.InnerXml

以 XML 格式获取元素及其内容:

xmlElements.OuterXml

【讨论】:

  • 我收到所有这 3 个示例的错误:'System.Collections.Generic.IEnumerable&lt;System.Xml.Linq.XElement&gt;' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'System.Collections.Generic.IEnumerable&lt;System.Xml.Linq.XElement&gt;' could be found (are you missing a using directive or an assembly reference?)
  • 在调用者中遍历列表后,您将获得这些值。您不能直接在 IEnumearble 对象上获取这些值。请参考@Scott Hannen 的帖子。
  • 将此添加到文件顶部:使用 System.Linq;
  • @Scott Hannen System.Linq; 已经存在 - 但出现错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多