1

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.

Kumar
  • 864
  • 1
  • 22
  • 46
  • possible duplicate of [Using XmlDocument to Retrieve values in c#](http://stackoverflow.com/questions/17396655/using-xmldocument-to-retrieve-values-in-c-sharp) – H H Jul 01 '13 at 10:54
  • @HenkHolterman Yes, It may Dupplicate. But People thought It is Console Or windows Application,That's why the question is going in other way, The `XmlDocument()` is varies in Store application. Thanks – Kumar Jul 01 '13 at 11:27
  • You can _edit_ a question (and the tags). – H H Jul 01 '13 at 11:45
  • @HenkHolterman ha,OK. What I thought is the namesapce of `XmlDocument()` is same for all .net platforms before I posted my first question. You suggesting me to Change my First question.. right? – Kumar Jul 01 '13 at 11:50

2 Answers2

3

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);
               }
            }
Rezoan
  • 1,745
  • 22
  • 51
  • I am getting this error **Cannot apply indexing with [] to an expression of type 'method group'** Plz see this [http://sdrv.ms/15an5Tq ] Why bcz `XmlDocument()` is different in Windows store apps,. – Kumar Jul 01 '13 at 11:07
  • Let me know can u write, itemNodes[0].Attributes["href"].Value – Rezoan Jul 01 '13 at 11:13
  • Try to write : XmlNodeList itemNodes = xmlDoc1.GetElementsByTagName("item"); if (itemNodes.Count > 0) {ids3list.Add(itemNodes[0].Attributes["href"].Value) } if it shows no error please knock me – Rezoan Jul 01 '13 at 11:14
  • No,I tried these things,It won't work. Thanks you for considering my question. – Kumar Jul 01 '13 at 11:23
  • You can look at the classes available for Windows Store apps in Visual Studio's Object Browser (on the View menu). If you search for "XmlNode" it will bring you to Windows.Data.Xml.Dom.XmlNodeList which should get you to the right area for everything you need. – Rezoan Jul 01 '13 at 11:26
  • may be http://msdn.microsoft.com/en-us/library/windows/apps/hh868253.aspx will help – Rezoan Jul 01 '13 at 11:30
  • try the new solution i wirte in the answer section @kumar or you can visit http://social.msdn.microsoft.com/Forums/windowsapps/en-US/65cffae7-a264-43a8-82e3-aa506d005ad3/get-element-values-from-xml-in-c there is a solution exist. – Rezoan Jul 01 '13 at 11:49
  • I tried It won't work, That msdn solution solution is non-predictable. I am trying use `XDocument` or any thing else – Kumar Jul 01 '13 at 13:01
0

For Windows Store XmlDocument to read / write an attribute value:

XmlAttribute attr = (XmlAttribute)attrColl.GetNamedItem("href");
attr.Value = "images/img11.jpg";
or
string val = attr.Value;

MSDN: XmlDocument.Attributes

Gerhard Powell
  • 5,965
  • 5
  • 48
  • 59