简体   繁体   中英

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

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..:)

With XmlDocument you could do the following...

XmlNodeList xnList = xd.SelectNodes("navMap/navPoint"); //xd being your xmldocument.   returns all "navPoint" nodes under navMap and navMap  is your root node
        foreach (XmlNode node in xnList)
        {
            string retText = node["navLabel"]["text"].InnerText;                 // navLabel/text
            string retContentAtt = node["content"].Attributes["src"].Value;    // navPoint/content src=" 
        }

I think this is what you are looking for. Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM