Reputation: 147
I have a specific function that I want to run, and it is located inside an XML File:
Console.WriteLine("Text for test, {0}, {1}", testWord, testWord2);
The text is stored in an XML file:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<world>
<region name="TestRegion">
<area name="TestArea">
<building name="Outside">
<room name="TutorialRoom">
<textToDisplay>"Text for test, {0},{1}"</textToDisplay>
<extraString>testWord,tesWord2</extraString>
</room>
</building>
</area>
</region>
</world>
</root>
I can easily get the string data using LINQ
XElement xelement = XElement.Load("..\\..\\LocationDatabase.xml");
var textToDisplay= xelement.Elements("world")
.Elements("region").Where(region => (string)region.Attribute("name") == "TestRegion")
.Elements("area").Where(area => (string)area.Attribute("name") == "TestArea")
.Elements("building").Where(building => (string)building.Attribute("name") == "Outside")
.Elements("room").Where(room => (string)room.Attribute("name") == "TutorialRoom")
.Elements("textToDisplay");
var extraString= xelement.Elements("world")
.Elements("region").Where(region => (string)region.Attribute("name") == "TestRegion")
.Elements("area").Where(area => (string)area.Attribute("name") == "TestArea")
.Elements("building").Where(building => (string)building.Attribute("name") == "Outside")
.Elements("room").Where(room => (string)room.Attribute("name") == "TutorialRoom")
.Elements("extraString");
My issue here is how I make the command `Console.WriteLine("Something {0}", extra); to accept both LINQ statements. Anybody have an idea?
Upvotes: 0
Views: 231
Reputation: 23937
how about this:
First split the extra string into a String[]
by using Split(',')
and then use a String.Format()
to display the textToDisplay
with the 2 strings in your string array.
var values = extraString.FirstOrDefault().Value.Split(',');
Console.WriteLine(String.Format(textToDisplay.First().Value, values[0], values[1]));
Upvotes: 1
Reputation: 28779
The call to Console.WriteLine()
is nothing special -- it just expects a string
and an object[]
. Your data is there, but buried in XElement
instances. So
Console.WriteLine(
(string) textToDisplay.First(),
((string) extraString.First()).Split(',')
);
does what you want.
Note that this only works when "extraString" is something simple like a comma-separated list. You can't put arbitrary expressions like "2 + 2" in there and expect that to work -- for that we'd need to invoke the compiler and actually produce code, which is much more complicated.
Upvotes: 2
Reputation: 43300
You can simply just pass in the strings as they are, however you will probably need to split the second one at the comma.
string one = "format me {0}{1}";
string two = "here, and here";
Console.WriteLine(one, two.Split(','));
Upvotes: 2