解析包含冒号的xml

时间:2014-08-02 18:39:21

标签: c# xml parsing

我有这个XML文本:

<test:solution xmlns:test="http://www.test.com/">
    <script/>
    <test:question>
        <test:param name="name1">value 1</test:param>
        <test:param name="name2"> value 2</test:param>
    </test:question>
</test:solution>

当我运行包含这些行的应用程序时:

XmlDocument doc = new XmlDocument();
doc.Load(xmlUrl);
XmlNode testQuestions = doc.SelectSingleNode("/test:solution/test:question");
//XmlNodeList testParamNodeList = testQuestions.SelectNodes("test:param");

它出错,我知道错误来自冒号,但我不知道如何解决它。
感谢您的帮助

1 个答案:

答案 0 :(得分:4)

试试这个:

    XmlDocument doc = new XmlDocument();

    doc.Load(PATH);

    XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
    ns.AddNamespace("test", "http://www.test.com/");
    XmlNode node = doc.SelectSingleNode("//test:solution//test:question", ns);