English 中文(简体)
从XML档案中捕获特定元素
原标题:catch particular element from XML file

xml:

<?xml version="1.0" encoding="utf-8" ?>
<StudentList>
<student>
  <name>Ram</name>
  <Id>1</Id>
</student>

  <student>
  <name>Hari</name>
  <Id>2</Id>
</student>
  <student>
    <name>Gita</name>
    <Id>3</Id>
  </student>
  <student>
    <name>Sita</name>
    <Id>4</Id>
  </student>
</StudentList>

页: 1

XmlDocument XD = new XmlDocument();
        XD.Load(System.Web.HttpContext.Current.Server.MapPath("XmlFile.xml"));
        XmlNodeList nodeListName = XD.GetElementsByTagName("student");
        foreach (XmlNode xNode in nodeListName)
        {
            foreach (XmlElement xelement in xNode)
            {
                PlaceHolder pshd = new PlaceHolder();
                LblDisplay.Text += xelement.FirstChild.Value + "   ";
                string Name += xelement.FirstChild.Value;
            }
            LblDisplay.Text += "<br/>";

        }

i 只想从上述xml档案中收集名字,而要获得姓名和复制。 怎么能够做,就会帮助。

最佳回答
XmlDocument xml = new XmlDocument();
xml.LoadXml(System.Web.HttpContext.Current.Server.MapPath("XmlFile.xml")); 

XmlNodeList xnList = xml.SelectNodes("/StudentList/student");
foreach (XmlNode xn in xnList)
{
  string name= xn["name"].InnerText;
  string Id= xn["Id"].InnerText;   
}
问题回答

不要在<条码>x Node上签字,你只是希望其<条码>。

foreach (XmlNode xNode in nodeListName)
{
    string name = xNode.FirstChild.InnerText;
    LblDisplay.Text += name + "<br/>";
}

也可以使用LLL(该系统)。 Xml.Linq namespace, using XDocument:

XDocument xDoc = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("XmlFile.xml"));

var names = from x in xDoc.Descendants("student")
            select x.Element("name");

foreach (XElement studentName in names)
{
    LblDisplay.Text += studentName.Value + " ";
}

LblDisplay.Text += "<br />";

如果你想要id,你可以将“Id”替换在<条码>中,代之以“电子名称”;。

或者,你可以抓住名字和密码,并将他们作为匿名的收集回来:

var students = from x in xDoc.Descendants("student")
               select new
                   {
                       name = x.Element("name").Value,
                       id = x.Element("Id").Value
                   };

因此,你可以接触:

foreach (var student in students)
{
    LblDisplay.Text += student.name + " student.Id + " ";
}

或者,你们想要做的是什么。

你们可以采用一切方式:

nodeList = XD.SelectNodes("//student/name")
foreach (XMlNode node  in nodeList){
    LblDisplay.Text += node.Value + "<br/>";
}
String fileName = @"C:Documents and Settingsaritra.ghoshDesktopStudent.xml";
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(fileName);
        XmlNodeList list = xDoc.SelectNodes("StudentList/student/name");
        foreach (XmlNode item in list)
        {
            Console.WriteLine(item.InnerText);
        }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签