[英]C# extract - XML Child Node Attributes
Sample of XML File XML文件样本
<NvM_DataManager_Blocks
AlignmentConstraint="ManagedBy_DataManager"
GenerateBlockAccessAPI="Yes" UseProtectedSections="Yes" UseRteForDataManagerInterface="No">
<DATA_MANAGER_EEPROM_BLOCK Block_Crc_Type="NVM_CRC16"
Block_Priority="255" Block_Type="NVM_BLOCK_NATIVE"
ErrorCallback="" FeeOrEa_BlockId=""
Name="DataManager_Block_01"
Project_specific_information=""
Resistant_To_Changed_SW="No" Storage_In="Fee" Write_Only_At_WriteAll="No">
<DATA_ELEMENT CreateCommitedApi="Yes" DataSize=""
DataSize_bit="" Data_type="uint8" Default_Value="66"
Header_file="" Name="DataManager_DataElement_01"
Number_Of_Planned_Writes="255" VariantInit="No"/>
<DATA_ELEMENT CreateCommitedApi="Yes" DataSize=""
DataSize_bit="" Data_type="uint32"
Default_Value="255" Header_file=""
Name="DataManager_DataElement_02"
Number_Of_Planned_Writes="363" VariantInit="No"/>
</DATA_MANAGER_EEPROM_BLOCK>
//Load xml XDocument xdoc = XDocument.Load("C:\\\\Users\\\\John\\\\Desktop\\\\Program\\\\file.xml"); var blocks2 = (from r in xdoc.Descendants("DATA_MANAGER_EEPROM_BLOCK") select new { Name = r.Attribute("Name").Value, //This line below does not the produce what is req. I need some help on how to fix this. Sub_Elements = xdoc.Descendants("DATA_MANAGER_EEPROM_BLOCK").Descendants("DATA_ELEMENT") }).ToList();
However, I have some issue to extract the sub elements : DataManager_DataElement_01 & DataManager_DataElement_02 and their attributes. 但是,提取子元素有一些问题:DataManager_DataElement_01和DataManager_DataElement_02及其属性。
Try following 尝试跟随
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("CrC_Type", typeof(string));
dt.Columns.Add("Priority", typeof(int));
dt.Columns.Add("Type", typeof(string));
dt.Columns.Add("Block_Name", typeof(string));
dt.Columns.Add("Data_Type", typeof(string));
dt.Columns.Add("Default_Value", typeof(int));
dt.Columns.Add("Element_Name", typeof(string));
dt.Columns.Add("Planned_Writes", typeof(int));
XDocument doc = XDocument.Load(FILENAME);
foreach (XElement block in doc.Descendants("DATA_MANAGER_EEPROM_BLOCK"))
{
string crcType = (string)block.Attribute("Block_Crc_Type");
int blockPriority = (int)block.Attribute("Block_Priority");
string blockType = (string)block.Attribute("Block_Type");
string blockName = (string)block.Attribute("Name");
foreach(XElement element in block.Elements("DATA_ELEMENT"))
{
string dataType = (string)element.Attribute("Data_type");
int defaultValue = (int)element.Attribute("Default_Value");
string elementName = (string)element.Attribute("Name");
int plannedWrites = (int)element.Attribute("Number_Of_Planned_Writes");
dt.Rows.Add(new object[] {
crcType,
blockPriority,
blockType,
blockName,
dataType,
defaultValue,
elementName,
plannedWrites
});
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.