Creating XML Using Linq for XML and Arrays

I use Linq To XML to create XML that is sent to a third party. I find it difficult to understand how to create XML using Linq when some of the information I want to send to XML is dynamic.

The dynamic part of XML is stored as an array [,]. This multidimensional array contains 2 values.

I can "build" dynamic XML up using a string constructor and store the values โ€‹โ€‹that were in the array in a string variable, but when I try to include this variable in Linq, the variable is HTMLEncoded, and not included as proper XML.

How do I add an add to a dynamically built string for XML generated by Linq?

Example:

//string below contains values passed into my class

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);

"" , XML , , - , .

, , :

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

- :

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

? XmlDocument, , Linq, .

,

+3
1

XElements ( "name", "value" ) - - , , .

XElement XML, XML, XElement.Load(). XML, , .

:

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

Source: https://habr.com/ru/post/1714795/


All Articles