Reputation: 67
I have a question.
Still working on a project, but i am stuck.
We had this code:
var copyitems = doc.Descendants(application) // Read all descendants
.SelectMany(x => x.Elements("Test"))
.Select(s =>
{
var splits = s.Value.Split(new string[] { "@SRCDIR@", "@INSTDIR@" }, StringSplitOptions.RemoveEmptyEntries); // split the string to separate source and destination.
return new { Source = splits[0].Replace(",", ""), Destination = splits[1].Replace(",", "") };
})
.ToList();
in combination with this xml example:
<root>
<Test>
<Copy>@SRCDIR@\test1 ,@INSTDIR@\test1</Copy>
</Test>
<root>
But the xml changed to this now:
<root>
<Test>
<Copy>
<Source>@SRCDIR@\test</Source>
<Destination>@INSTDIR@\test1</Destination>
</Copy>
</Test>
<root>
How can i now do the same, but instead of making a splits just read the info of source and destination.
Also you can have multiple copy descedants and also descedants with other names which don't have the source and destination value
Upvotes: 2
Views: 292
Reputation: 28272
Your linq query should actually work the same for both Xml (at least the ones shown in your question).
only fixed the invalid </root>
, the linq code (I removed application
since we don't have that) is exactly the same
If your Xml is different then please show one where your code wouldn't work
Note that element.Value
will contain only the non-tags, so for the first Xml
you are splitting on: @SRCDIR@\test1 ,@INSTDIR@\test2
, and on the second example you are splitting on @SRCDIR@\test1@INSTDIR@\test2
, but since the separator is consistent, it just works.
Upvotes: 1
Reputation: 15663
You only have to do a minor change, but still use the Split
method. Just add the comma in the separators list.
var separators = new string[] { "@SRCDIR@", "@INSTDIR@", "," };
var tokens = s.Value.Split(separators, StringSplitOptions.RemoveEmptyEntries);
var source = tokens[0];
var destination = tokens[1];
Upvotes: 1
Reputation: 16956
Look for Element
method which accepts element name as an input.
var copyitems = doc.Descendants(application) // Read all descendants
.SelectMany(x => x.Elements("Test"))
.Select(s =>
{
return new
{
Source = s.Element("Source").Value.Replace("@SRCDIR@", ""),
Destination =s.Element("Destination").Value.Replace("@INSTDIR@", "")
};
})
.ToList();
Upvotes: 1