简体   繁体   English

从XML获取标签中的标签值

[英]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. 我在Xml文件的Content下面,我试图获取<text><content .. />标记的值,这些值仅在标记<navmap> ... </navmap>

I am using XmlDocument() of nameSpace using Windows.Data.Xml.Dom; 我正在using Windows.Data.Xml.Dom; nameSpace的XmlDocument() 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. 我以前使用过XmlDocument() ,但是这种类型的XMl内容却大不相同,我不知道必须为标记中的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.. 我正在使用c#尝试使用Windows商店应用程序,就像这样。

            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; 谁熟悉using Windows.Data.Xml.Dom;的nameSpace的XmlDocument() 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 U可以通过this获取文本。字符串只是为了表明您可以获取内部文本。如果您要将所有值存储在text标签下。使用列表并添加其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... 使用XmlDocument,您可以执行以下操作...

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 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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