在标签中显示XML内容 [英] Display XML Conten in a label

查看:87
本文介绍了在标签中显示XML内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在标签中显示xml内容,以下是xml内容

How can i display an xml content in a label the following is the xml content

<?xml version="1.0" encoding="utf-8" ?>
<Books>
    <Text>
       <Subject>
           Alpha
       </Subject>
       <Content>
           Bravo
       </Content>
    </Text>
<Books>



我想将内容显示为

主题:Alpha
内容:Bravo


在两个不同的标签中



I want to display the content as

Subject:Alpha
Content:Bravo


in two different labels

推荐答案

写一个类似于此&的LINQ.试试

Write a LINQ similar to this & try

// Create the query
            var books = from c in System.Xml.Linq.XElement.Load("Books.xml").Elements("Text")
                        select c;
            if (books != null)
            {
                foreach (var b in books)
                {

                    string subjectvalue = b.Element("Subject").Value; //will give you Alpha
                }
            }


所以您要面对什么问题,您需要解析XML并访问适当的节点,并将其分配给Label的text属性.

您可以使用XPath查询XML.例如,查看链接
So what problem you are facing, You need to parse the XML and access the appropriate nodes and assign it to Label''s text property.

You can query to your XML using XPath. Have a look to the link for example


您可以使用类似
You may use something like
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("mydata.xml");
label1.Text = xmldoc.SelectSingleNode("/Books/Text/Subject").FirstChild.Value;
label2.Text = xmldoc.SelectSingleNode("/Books/Text/Content").FirstChild.Value;



只要您正确修复了XML文档(底部节点应为</Books>)



Provided you correctly fix your XML document (the bottom node should be </Books>)


这篇关于在标签中显示XML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆