I am using the XDocument
class and LINQ to XML to create some simple HTML output, and I stumbled over a problem that I can't seem to solve. Perhaps it isn't even possible at all when using XML.
I have a POCO with a lot of properties, and some of them are named by conventions, so that I use Reflection to iterate through them similar to what follows:
List<string> propSuffixes = new List<string>();
for (int i=1; i<=20; i++)
{
propSuffixes.Add(i.ToString("D2")); // "01", "02", ..., "20"
}
XDocument xDoc = new XDocument(
new XElement("html",
new XElement("body",
propSuffixes
.Select(suffix =>
(string)typeof(MyPOCO)
.GetProperty("p" + suffix)
.GetValue(myPocoInstance))
.Select(v =>
new XElement("span", v, new XElement("br"))))));
This puts the string values of the 20 properties in their own <span>
together with a <br />
tag. But I'd like to have "just" the string values without the span, but still with the <br />
at the end of each line. That is, like this:
<html>
<body>
propertyValue1<br />
propertyValue2<br />
...
propertyValue20<br />
</body>
</html>
Is this even possible, or is this against some XML validation rule? And if it's possible, how can I do it with LINQ? The difficulty for me lies in having to return 2 object
parameters to the arguments of new XElement("body",...)
, but also I don't know how to pass just that string
before the <br />
to the arguments without raising some errors.
Do you know of any way to accomplish my goal, or alternatively to accomplish a similar layout (perhaps I could use <p>
instead of string
plus <br />
)?
Aucun commentaire:
Enregistrer un commentaire