简体   繁体   English

将xml字符串传递给System.Xml.Linq.XElement时,避免使用尖括号将xml转义

[英]Avoid xml escaping of angle brackets, when passing xml string to System.Xml.Linq.XElement

I'm getting a string from string GetXmlString() ; 我从string GetXmlString()得到一个字符串; this I cant change. 这我不能改变。

I have to append this to an xml within a new XElement ("parent" , ... ); 我必须将此附加到new XElement ("parent" , ... );的xml中new XElement ("parent" , ... ); , to the ... area. ,到...区域。

This string I'm getting is of the following format. 我得到的该字符串具有以下格式。

<tag name="" value =""></tag>
<tag name="" value =""></tag>
<tag name="" value =""></tag>
...

The final result I want is this to be like 我想要的最终结果是这样

<parent>
<tag name="" value =""></tag>
<tag name="" value =""></tag>
<tag name="" value =""></tag>
<tag name="" value =""></tag>
<tag name="" value =""></tag>
...
</parent>

when I just pass the string as XElement("root", GetXmlString()) < and > are encoded to &lt; 当我仅将字符串作为XElement("root", GetXmlString())传递时XElement("root", GetXmlString()) <>编码为&lt; and &gt &gt

When I try XElement.Parse(GetXmlString()) or XDocument.Parse(GetXmlString()) I get the There are multiple root elements exception. 当我尝试XElement.Parse(GetXmlString())XDocument.Parse(GetXmlString()) ,出现“ 存在多个根元素”异常。

How do I get the required output without escaping the brackets? 我如何在不逃脱括号的情况下获得所需的输出? What am I missing? 我想念什么?

The simplest option is probably to give it a root element, then parse it as XML: 最简单的选择可能是它一个根元素,然后将其解析为XML:

var doc = XDocument.Parse("<parent>" + text + "</parent>");

If you need to append to an existing element, you can use: 如果需要附加到现有元素,则可以使用:

var elements = XElement.Parse("<parent>" + text + "</parent>").Elements();
existingElement.Add(elements);

An alternative to Jon's suggestion would be to create an XmlReader for your fragment and parse from that: Jon建议的替代方法是为您的片段创建一个XmlReader并从中进行解析:

var element = new XElement("parent");

var settings = new XmlReaderSettings
{
    ConformanceLevel = ConformanceLevel.Fragment
};

var text = GetXmlString();

using (var sr = new StringReader(text))
using (var xr = XmlReader.Create(sr, settings))
{
    xr.MoveToContent();

    while (!xr.EOF)
    {
        var node = XNode.ReadFrom(xr);   
        element.Add(node);
    }
}

This would be useful if the 'parent' element already exists, else simple concatenation of the XML nodes at each end and parsing would be simpler. 如果“父”元素已经存在,这将很有用,否则在两端的XML节点的简单串联和解析将更加简单。

暂无
暂无

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

相关问题 System.Xml.Linq.XElement中的查询元素 - Query elements in System.Xml.Linq.XElement System.Xml.Linq.XElement&#39;不包含&#39;XPathEvaluate的定义 - System.Xml.Linq.XElement' does not contain a definition for 'XPathEvaluate 扩展System.Xml.Linq.XElement-内部CloneNode - Extending System.Xml.Linq.XElement - internal CloneNode 如何使用XmlWriter将System.Xml.Linq.XElement写入流 - How to write System.Xml.Linq.XElement using XmlWriter to a stream 如何反序列化 System.Xml.Linq.XElement? - How I can deserialize a System.Xml.Linq.XElement? 生成UWP时,System.Xml.Linq.XElement没有Load方法 - System.Xml.Linq.XElement does not have Load method when building UWP 对Xdocument的Linq查询返回“System.linq.Enumerable + WhereSelectEnumerableIterator&#39;2 [system.XML.Linq.Xelement,System.String] - Linq query on Xdocument returns "System.linq.Enumerable+WhereSelectEnumerableIterator'2[system.XML.Linq.Xelement,System.String] VS2010将System.Xml.XmlElement与System.Xml.Linq.XElement混淆? - VS2010 confuses System.Xml.XmlElement with System.Xml.Linq.XElement? Web服务:无法将类型&#39;System.Xml.Linq.XElement&#39;隐式转换为&#39;System.Xml.XmlElement&#39; - Webservices : Cannot implicitly convert type 'System.Xml.Linq.XElement' to 'System.Xml.XmlElement' 错误:无法将类型为“ WhereSelectEnumerableIterator`2 [System.Xml.Linq.XElement,System.String]”的对象强制转换为“ System.IConvertible”类型 - Error: Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String]' to type 'System.IConvertible'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM