Search code examples
c#microsoft-metrowindows-store-appsxmldocument

Use XmlDocument to get XML data in Metro apps


It seems silly for someone. I working on this last few days still I am not yet get success. I working for Windows Store application using c#. I am trying to parse the XMl file from local.

    <?xml version="1.0" ?>
<package version="2.0" xmlns="http://www.idpf.org/2007/opf" unique-identifier="BookId">
  <metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
   <dc:title>Cinderella; Or The Little Glass Slipper</dc:title>
   <dc:creator>Anonymous</dc:creator>
   <dc:date>2009-10-14</dc:date>
   <dc:subject>Youth</dc:subject>
   <dc:language>en</dc:language>
   <dc:publisher>Web Books Publishing</dc:publisher>
   <dc:identifier id="BookId">web-books-154</dc:identifier>
 </metadata>
 <manifest>
     <item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml" />
     <item id="W000Title" href="000Title.html" media-type="application/xhtml+xml" />
     <item id="W01MB154" href="01MB154.html" media-type="application/xhtml+xml" />
     <item id="WTOC" href="TOC.html" media-type="application/xhtml+xml" />
    <item id="style" href="style.css" media-type="text/css" />
    <item id="cover" href="cover.jpg" media-type="image/jpeg" />
      <item id="Wimg11" href="images/img11.jpg" media-type="image/jpeg" />
      <item id="Wimg12" href="images/img12.jpg" media-type="image/jpeg" />
      <item id="Wimg2" href="images/img2.jpg" media-type="image/jpeg" />
      <item id="Wimg4" href="images/img4.jpg" media-type="image/jpeg" />
      <item id="Wimg6" href="images/img6.jpg" media-type="image/jpeg" />
      <item id="Wimg7" href="images/img7.jpg" media-type="image/jpeg" />
      <item id="Wimg9" href="images/img9.jpg" media-type="image/jpeg" />
 </manifest>
  <spine toc="ncx">
    <itemref idref="W000Title" />
    <itemref idref="W01MB154" />
 </spine>
</package>

From the above Xml file I want href Values in For this I am XmlDocument(). Here is my c# Code.

XmlNodeList itemref = xmlDoc1.GetElementsByTagName("itemref");
            foreach (XmlElement idref in itemref)
            {
                if (idref.Attributes.Count > 0)
                {
                    XmlAttribute Str1 = idref.GetAttributeNode("idref");
                    ids1list.Add(Str1.Value); //List
          }
            }
XmlNodeList itemNodes = xmlDoc1.GetElementsByTagName("item");
           foreach (XmlElement item in itemNodes)
            {
                    XmlAttribute str2 = item.GetAttributeNode("id");
                    ids2list.Add(str2.value);
                    XmlAttribute str3 = item.GetAttributeNode("href");
                    ids3list.Add(str3.value);
             }
            var resut = ids1list.Intersect(ids2list).ToList();

In the resut list I have the values of id elements. I want next value which is href using id value, Is there any way to get next element value,. Thanks.


Solution

  • You can do this by following code,

                XmlNodeList itemNodes = xmlDoc1.GetElementsByTagName("item");
                if (itemNodes.Count > 0)
                {
                   foreach (XmlElement node in itemNodes)
                   {
                     ids3list.Add(node.Attributes["href"].Value);
                   }
                }