3

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

<item id="W01MB154" href="01MB154.html" media-type="application/xhtml+xml" />
<item id="W000Title" href="000Title.html" media-type="application/xhtml+xml" />

with

  <itemref idref="W000Title" />
  <itemref idref="W01MB154" />

These values in same file.

Here I know the value of id within the tag of item, after that I want to know the value of the href element.

What I have to do is compare the value idref element in the itemref tag with element value of id in the tag item. Here I know the id value which is W01MB154.

Simply, I want to know the next value of id which is href element, Using XmlDocument().

Dale K
  • 25,246
  • 15
  • 42
  • 71
Kumar
  • 864
  • 1
  • 22
  • 46
  • Can you post complete xml file or at least other tags surrounding item tag? There must be single root element but in above example there are more – Munawar Jul 01 '13 at 02:31
  • @theghostofc Sorry to say the `XMlDocument()` is totally varies in Store app. I checked your code as Console app it is looking good. It is my mistake with out mentioning as Store app. I am apologize for that, I am changing the question. Thanks. – Kumar Jul 01 '13 at 11:55

2 Answers2

6

Following code loads and parses content.opf file without any error.

To iterate and compare above xml you may use following code:

try
{
    XmlDocument xDoc = new XmlDocument();
    xDoc.Load("content.opf");

    XmlNodeList items = xDoc.GetElementsByTagName("item");
    foreach (XmlNode xItem in items)
    {
        string id = xItem.Attributes["id"].Value;
        string href= xItem.Attributes["href"].Value;
    }
}
catch (Exception ex)
{
    System.Diagnostics.Debug.WriteLine(ex.Message);
}
Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
Munawar
  • 2,588
  • 2
  • 26
  • 29
  • Here Is my file [http://sdrv.ms/14HtQdi ] Right now I am doing like this... I am not using **Root node.** XmlNodeList itemref = xmlDoc1.GetElementsByTagName("itemref"); foreach (XmlElement idref in itemref) { if (idref.Attributes.Count > 0) { XmlAttribute Str1 = idref.GetAttributeNode("idref"); ids1 = Str1.Value; for (int i = 0; i < itemref.Count; i++) { ids1.ToCharArray(); } After looking ur code I am going to change it., Can you help me for this.. Why because I am new to this, Thanks – Kumar Jul 01 '13 at 02:51
  • When I try to use your code snippet I am getting an error, **The best overloaded method match for 'Windows.Data.Xml.Dom.XmlNamedNodeMap.this[int]' has some invalid arguments**, Before that I am not getting `XmlNode`, Instead of this I am using `IXmlNode`. – Kumar Jul 01 '13 at 05:10
  • There is no `Load` Definition in `XmlDOcument()`. Can U please check this.. [http://sdrv.ms/1b1lzpf ] If I use `.LoadXml()` It is giving error in `xItem.Attributes["id"].Value;` **Cannot apply indexing with [] to an expression of type 'method group'** – Kumar Jul 01 '13 at 08:44
  • You need to add reference to System.Xml; or use fully qulified name for xmlDocument class like this. System.Xml .XmlDeocumnt xDoc= new System.Xml .XmlDeocumnt(); – Munawar Jul 01 '13 at 09:03
  • The only issue is, you are developing a store app where it reference Window.Data.Xml namespace instead system.xml. You did not specified anywhere that you are working on store app and you want it to process xml some different way. whatever, if you just use fully qualified name for XmlDocument, it should be able to work for you (as mentioned in my last comments) – Munawar Jul 01 '13 at 11:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32697/discussion-between-kumar-and-needo) – Kumar Jul 01 '13 at 18:30
6

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

string str = @"<?xml version='1.0' encoding='UTF-8'?><root><items><item id='W01MB154' href='01MB154.html' media-type='application/xhtml+xml' /><item id='W000Title' href='000Title.html' media-type='application/xhtml+xml' /></items><itemrefs><itemref idref='W000Title' /><itemref idref='W01MB154' /></itemrefs></root>";
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:

<?xml version='1.0' encoding='UTF-8'?>
<root>
    <items>
        <item id='W01MB154' href='01MB154.html' media-type='application/xhtml+xml' />
        <item id='W000Title' href='000Title.html' media-type='application/xhtml+xml' />
    </items>
    <itemrefs>
        <itemref idref='W000Title' />
        <itemref idref='W01MB154' />
    </itemrefs>
</root>

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

Community
  • 1
  • 1
Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
  • When I try to use your code snippet I am getting an error, The best overloaded method match for 'Windows.Data.Xml.Dom.XmlNamedNodeMap.this[int]' has some invalid arguments, Before that I am not getting XmlNode, Instead of this I am using IXmlNode, May be I working on WIndows metro apps, that why it is not working of `XmlNode` I need a suggestion from you, I am going to use `Linq to SQL` or `XDocument()`. Can you suggest me Which way is suitable for my situation or any thing else is there? – Kumar Jul 01 '13 at 06:59
  • You may also try `XmlNodeList itemRefList = xml.GetElementsByTagName("itemref"); – Vivek Jain Jul 01 '13 at 08:58
  • Yes, I am using `GetElementsByTagName("itemref");` only. Here I am structured. I Know the values of **id** in the List. Is there any property to get the next element value which is **href** in `XmlDocument()`. give me any idea. Thanks – Kumar Jul 01 '13 at 09:30
  • @kumar, check out my edit. It runs well for me and gives me the desired output in a console application. Please check the `structure of the XML document` and the `XPath expression`. – Vivek Jain Jul 01 '13 at 10:10
  • Sorry to say the `XMlDocument()` is totally varies in Store app. I checked your code as Console app it is looking good. It is my mistake with out mentioning as Store app. I am apologize for that. Thanks. – Kumar Jul 01 '13 at 11:53
  • Did you try out with `XDocument` or `LINQ to XML`? – Vivek Jain Jul 01 '13 at 11:55
  • No,not yet. I am going to start for that. If you don't mind Can you give any idea. – Kumar Jul 01 '13 at 11:57