从XML结构中读取数据使用c# [英] read the data from XML Structure using c#

查看:221
本文介绍了从XML结构中读取数据使用c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将xml文件存储在一个字符串对象中,我将xml结构存储在c#中的局部变量字符串abcd 中。

I store the xml file in one string object like,I stored the xml structure in local variable string abcd in c#.

 <structure>
    <a>Test Name</a>
    <e>test address</e>
    <c>
       <c1>yyyy<c1>
       <c2>xxxx</c2>
    </c>
    </structure>

如何使用c#读取(解析)此xml字符串,并存储标签a和标签c1,使用c#在局部变量中标记c2值。

How to read(parse) this xml string using c# and store the tag a,and tag c1 ,tag c2 values in local variable using c#.

我试过像

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(abcd);

        XmlElement element = (XmlElement)xmldoc.GetElementById("a");

但是我得到null value.how从xml结构中读取值并存储在局部变量使用c# ?

but i get null value.how to read the values from xml structure and stored in local variable using c#?

推荐答案

Linq2Xml更容易使用。

Linq2Xml is much easier to use.

var xElem = XElement.Parse(abcd);
var a = xElem.Element("a").Value;
var c = xElem.Element("c").Element("c1").Value;

这篇关于从XML结构中读取数据使用c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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