現在、XMLリストから名前をランダムに選択してコンソールに出力しようとしています。ただし、ノードは常にnullと思われます。C#/ Monogame - XMLから単一ノードを読み込むと常にNULLが返される
<?xml version="1.0" encoding="utf-8"?>
<XnaContent xmlns:ns="Microsoft.Xna.Framework">
<Asset Type="Object">
<nameData>
<firstName>
<name>Charles</name>
<name>David</name>
<name>Bob</name>
<name>John</name>
</firstName>
</nameData>
</Asset>
</XnaContent>
とC#:あなたの助けを事前に
//create XML document
XmlDocument doc = new XmlDocument();
//load in XML file to doc
doc.Load("Content/XML/Names.xml");
Random rand = new Random();
int count = 1;
//Set count to be the number of name nodes in the first name field
count = doc.SelectNodes("//firstName/name").Count;
//set randVal so it never exceeds amount of name nodes
int randVal = rand.Next(count);
// set objNode to the name at position()
XmlNode objNode = doc.SelectSingleNode("/nameData/firstName/name[position() = " + rand + "]");
//Write the randomly chosen name to console
Console.WriteLine(objNode.InnerText);
おかげ
可能な重複します(http:// stackoverflowの.com/questions/17161317/xml-document-selectinginglenode-returns-null) –
'/ ...'はルートノード*から*を選択するので、 '/ nameData'は存在しません。 –