Using XmlDocument to Retrieve values in c#

后端 未结 2 557
灰色年华
灰色年华 2021-01-13 18:14

I am using XmlDocument() for parsing a file like **.opf ** for my application of EpubReader.



        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-13 19:00

    You may start with the following code. When I execute it, I get proper output:

    string str = @"";
    XmlDocument xml = new XmlDocument();
    xml.LoadXml(str);  // suppose that str string contains the XML data. You may load XML data from a file too.
    
    XmlNodeList itemRefList = xml.GetElementsByTagName("itemref");
    foreach (XmlNode xn in itemRefList)
    {
        XmlNodeList itemList = xml.SelectNodes("//root/items/item[@id='" + xn.Attributes["idref"].Value + "']");
        Console.WriteLine(itemList[0].Attributes["href"].Value);
    }
    

    Output:

    000Title.html

    01MB154.html

    The XML used is:

    
    
        
            
            
        
        
            
            
        
    
    

    Check the structure of the XML document and the XPath expression.

提交回复
热议问题