Search code examples
c#windows-store-appsxmldocument

Getting tag values with in the Tags from XML


I have below Xml file Content, I am trying to get values of <text> & <content .. /> tag which are inside the tag <navmap> ... </navmap> only.

I am using XmlDocument() of nameSpace using Windows.Data.Xml.Dom;

I worked with XmlDocument() earlier, But this type of XMl content is quite different, I am not getting idea which property I have to use for Tag value with in the tag.

      <docTitle>
         <text>XXXXXXX</text>
      </docTitle>
  <navMap>
    <navPoint id="navpoint-1" playOrder="1">
      <navLabel>
        <text>Title Page</text>
      </navLabel>
        <content src="000.html" />
    </navPoint>
    <navPoint id="navpoint-2" playOrder="2">
      <navLabel>
        <text>Main Text</text>
      </navLabel>
        <content src="01M.html" />
    </navPoint>
  </navMap>

I am Working With Windows store apps using c# I tried like this..

            using Windows.Data.Xml.Dom;
             ---------------
             ---------------
             ---------------

            StorageFile tocFile = await finalfolder.GetFileAsync(tocFileValue);
            string fileContents1 = await FileIO.ReadTextAsync(tocFile);
            string encodedContent1 = fileContents1.Replace("&nbsp;", "&#160;");
            tocDocument.LoadXml(encodedContent1,loadSettings1);
            XmlNodeList tocNodeList = tocDocument.GetElementsByTagName("navMap");
            foreach (XmlElement Element in tocNodeList)
            {
                //Element is showing as null..
            }   

Who are Familiar with XmlDocument() of nameSpace using Windows.Data.Xml.Dom; give me Suggestion.

Thanks


Solution

  • You can Simply do this...

     XmlDocument xml = new XmlDocument();
            xml.LoadXml(urXml);
    
        XmlNodeList textlist = xml.GetElementsByTagName("text");
        XmlNodeList contentList = xml.GetElementsByTagName("content");
    
        for (int i = 0; i < textlist.Count; i++)
        {
            string s1 = textlist[i].InnerText; //
        }
        for (int j = 0; j < contentList.Count; j++)
        {
            string s2 = contentList[j].InnerText;
        }
    

    U can get the text through this..string is taken just to show tht u can get the inner text..if you want to store all the values under text tag..use list and Add their innerText

    like:-

    for (int i = 0; i < textlist.Count; i++)
    {
    if(i==0)
    List<string> str=new list<string>();
    
    str.Add(textlist[i].InnerText);
    }
    

    same the case with content tag..

    Hope This Helps..:)