简体   繁体   English

使用Linq to XML和数组创建XML

[英]Create XML using Linq to XML and arrays

I am using Linq To XML to create XML that is sent to a third party. 我正在使用Linq To XML来创建发送给第三方的XML。 I am having difficulty understanding how to create the XML using Linq when part of information I want to send in the XML will be dynamic. 当我想在XML中发送的部分信息是动态的时,我很难理解如何使用Linq创建XML。

The dynamic part of the XML is held as a string[,] array. XML的动态部分保存为字符串[,]数组。 This multi dimensional array holds 2 values. 该多维数组包含2个值。

I can 'build' the dynamic XML up using a stringbuilder and store the values that were in the array into a string variable but when I try to include this variable into Linq the variable is HTMLEncoded rather than included as proper XML. 我可以使用stringbuilder“构建”动态XML,并将数组中的值存储到字符串变量中,但是当我尝试将此变量包含到Linq中时,变量是HTMLEncoded而不是作为正确的XML包含。

How would I go about adding in my dynamically built string to the XML being built up by Linq? 我如何将我的动态构建的字符串添加到由Linq构建的XML中?

For Example: 例如:

//string below contains values passed into my class

string[,] AccessoriesSelected; string [,] AccessoriesSelected;

//I loop through the above array and build up my 'Tag' and store in string called AccessoriesXML


//simple linq to xml example with my AccessoriesXML value passed into it
XDocument RequestDoc = new XDocument(
 new XElement("MainTag",
 new XAttribute("Innervalue", "2")
 ),
AccessoriesXML);

'Tag' is an optional extra, it might appear in my XML multiple times or it might not - it's dependant on a user checking some checkboxes. 'Tag'是一个可选的附加功能,它可能会多次出现在我的XML中,也可能不会出现 - 它取决于用户检查一些复选框。

Right now when I run my code I see this: 现在当我运行我的代码时,我看到了这个:

<MainTag> blah blah </MainTag>
&lt ;Tag&gt ;&lt ;InnerTag&gt ; option1="valuefromarray0" option2="valuefromarray1" /&gt ;&lt ;Tag/&gt ;

I want to return something this: 我想回复一下这个:

<MainTag> blah blah </MainTag>
<Tag><InnerTag option1="valuefromarray0" option2="valuefromarray1" /></Tag>
<Tag><InnerTag option1="valuefromarray0" option2="valuefromarray1" /></Tag>

Any thoughts or suggestions? 有什么想法或建议吗? I can get this working using XmlDocument but I would like to get this working with Linq if it is possible. 我可以使用XmlDocument来解决这个问题,但是如果有可能的话,我想让它与Linq一起使用。

Thanks for your help, Rich 谢谢你的帮助,Rich

Building XElements with the ("name", "value") constructor will use the value text as literal text - and escape it if necessary to achieve that. 使用(“name”,“value”)构造函数构建XElements将使用值文本作为文本文本 - 并在必要时将其转义以实现该目的。

If you want to create the XElement programatically from a snippet of XML text that you want to actually be interpreted as XML, you should use XElement.Load() . 如果要以希望实际解释为XML的XML文本片段以编程方式创建XElement,则应使用XElement.Load() This will parse the string as actual XML, instead of trying to assign the text of the string as an escaped literal value. 这会将字符串解析为实际的XML,而不是尝试将字符串的文本指定为转义的文字值。

Try this: 尝试这个:

XDocument RequestDoc = new XDocument(
 new XElement("MainTag",
 new XAttribute("Innervalue", "2")
 ),
 XElement.Load(new StringReader(AccessoriesXML)));

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

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