[英]How can I get text and attributes values in my 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>
請使用System.Xml.Linq如何獲取我所有不同節點的不同屬性值和文本(例如,名稱,num_brand和啟用品牌,啟用,類型和“ reg”進行處理)?
謝謝 !
System.Xml.Linq
命名空間比System.Xml
命名空間好得多。 您的XDocument
具有一個XElement
,而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);
您有很多方法可以做到這一點。 其中之一是XmlDocument。
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(myXML);
foreach(XmlNode node in xmlDoc.DocumentElement.ChildNodes){
string text = node.InnerText; //you can loop through children
}
看一下這篇文章: 如何在C#中讀取和解析XML文件?
性格方面,我喜歡Linq To Xml方法,請在此處獲取更多信息: https : //msdn.microsoft.com/en-us/library/bb387061.aspx
嘗試這個
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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.