2015-12-22 114 views
0

XML例如:我怎样才能获得文字,在我的XML属性值

<?xml version="1.0" encoding="utf-8" ?> 
<brand name="brand1" num_brand="118" enabled="True"> 
    <price> 
    <nodePattern>pattern</nodePattern> 
    <attribute type="text" ></attribute> 
    <treatment enabled="1" type="Regex">reg</treatment> 
    </price> 
    <title> 
    <nodePattern>pattern</nodePattern> 
    <attribute type="text" ></attribute> 
    <treatment enabled="1" type="Regex">reg</treatment> 
    </title> 
</brand> 

请,我怎么能得到不同的属性值和文本为我所有的不同节点(例如名称,num_brand和启用品牌,启用,输入和“reg”治疗)使用System.Xml.Linq?

谢谢!

+1

你尝试过这么远吗?你在挣扎什么?你想如何获得这些值(在一个字符串?加载到一个类?) –

+0

现在的问题是你的问题是什么?根据你想要的,Linq to XML很容易。例如'var x = doc.Root.Attribute(“num_brand”)。Value;'给你那个属性的文本.. – Stefan

回答

0

你有很多方法可以做到这一点。其中之一是XmlDocument。

XmlDocument xmlDoc = new XmlDocument(); 
xmlDoc.LoadXml(myXML); 
foreach(XmlNode node in xmlDoc.DocumentElement.ChildNodes){ 
    string text = node.InnerText; //you can loop through children 
} 

就以这个帖子一看: How do I read and parse an XML file in C#?

Personnaly我喜欢的LINQ到XML的方法,更多的相关信息在这里: https://msdn.microsoft.com/en-us/library/bb387061.aspx

+2

你的答案建议使用'XmlDocument',它不使用'System.Xml.Linq'(什么问题是关于)。它还指定如何获取'InnerText'而不是属性值...... –

1

System.Xml.Linq命名空间比System.Xml命名空间更好。你的XDocument有一个XElement,它反过来有儿童元素。每个元素都有属性和值。

下面是一个例子给你:

var text = @"<?xml version=""1.0"" encoding=""utf-8"" ?> 
      <brand name=""brand1"" num_brand=""118"" enabled=""True""> 
       <price> 
       <nodePattern>pattern</nodePattern> 
       <attribute type=""text"" ></attribute> 
       <treatment enabled=""1"" type=""Regex"">reg</treatment> 
       </price> 
       <title> 
       <nodePattern>pattern</nodePattern> 
       <attribute type=""text"" ></attribute> 
       <treatment enabled=""1"" type=""Regex"">reg</treatment> 
       </title> 
      </brand>"; 

XDocument document = XDocument.Parse(text); 

// one root element - "brand"   
System.Diagnostics.Debug.Assert(document.Elements().Count() == 1); 
XElement brand = document.Element("brand"); 

// brand has two children - price and title   
foreach (var element in brand.Elements()) 
    Console.WriteLine("element name: " + element.Name); 

// brand has three attributes 
foreach (var attr in brand.Attributes()) 
    Console.WriteLine("attribute name: " + attr.Name + ", value: " + attr.Value); 
0

试试这个

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string FILENMAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENMAME); 

      var brand = doc.Descendants("brand").Select(x => new 
      { 
       name = x.Attribute("name").Value, 
       num_brand = x.Attribute("num_brand").Value, 
       enabled = x.Attribute("enabled").Value, 
       nodePattern = x.Element("price").Element("nodePattern").Value, 
       attribute = x.Element("price").Element("attribute").Attribute("type").Value, 
       priceTreatmentEnable = x.Element("price").Element("treatment").Attribute("enabled").Value, 
       priceTreatmentType = x.Element("price").Element("treatment").Attribute("type").Value, 
       priceTreatment = x.Element("price").Element("treatment").Value, 
       titleTreatmentEnable = x.Element("title").Element("treatment").Attribute("enabled").Value, 
       titleTreatmentType = x.Element("title").Element("treatment").Attribute("type").Value, 
       titleTreatment = x.Element("title").Element("treatment").Value 
      }).FirstOrDefault(); 
     } 
    } 
} 
​