[英]c# XML Parsing separating innerxml from innertext
What I am trying to do is create ideally a nested List basically a 2d list, or a 2D array if that is better for this task, that would work as follows ID => 1 Name => Hickory
without explicitly selecting the node. 我想做的是理想地创建一个嵌套列表,基本上是一个2d列表,或者创建一个2D数组(如果这样做更适合此任务),将可以按以下方式工作:
ID => 1 Name => Hickory
而无需显式选择节点。
I could use SelectNode (Woods/Wood) and then do something like node["ID"].InnerText
but that would require that I know what the nodes name is. 我可以使用SelectNode(Woods / Wood),然后执行诸如
node["ID"].InnerText
但这需要我知道节点名称是什么。
Assume that this would read wood.xml
even if there were 36 nodes instead of 7 and that I will never know the name of the nodes. 假设即使有36个节点而不是7个节点,这也将读取
wood.xml
,并且我永远都不知道节点的名称。 I tried using outerxml
/ innerxml
but that gives me too much information. 我尝试使用
outerxml
/ innerxml
但这给了我太多信息。
XmlDocument doc = new XmlDocument();
doc.Load("wood.xml");
//Here is wood.xml
/*<Woods><Wood><ID>1</ID><Name>Hickory</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>2</ID><Name>Soft Maple</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>3</ID><Name>Red Oak</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood></Woods>*/
XmlNode root = doc.FirstChild;
//Display the contents of the child nodes.
if (root.HasChildNodes)
{
for (int i=0; i<root.ChildNodes.Count; i++)
{
Console.WriteLine(root.ChildNodes[i].InnerXml);
Console.WriteLine();
}
Console.ReadKey();
}
That would allow me to basically create a wood " buffer " if you will so I can access these values elsewhere. 如果可以的话,这将允许我基本上创建一个木头“ 缓冲区 ”,以便我可以在其他位置访问这些值。
Sorry if I was unclear I want to essentially make this "abstract" for lack of a better word. 抱歉,如果我不清楚,我想本质上说这个“摘要”是因为缺少更好的词。
So that if I were someday to change the name of "Weight" to "HowHeavy" or if i were to add an additional element "NumberOfBranches" I would not have to hardcode the structure of the xml file. 这样一来,如果我有一天将“重量”的名称更改为“ HowHeavy”,或者如果我要添加一个附加元素“ NumberOfBranches”,则无需对xml文件的结构进行硬编码。
Is this what you after ? 这是你所追求的吗?
class Program
{
static void Main(string[] args)
{
string xml = @"<Woods><Wood><ID>1</ID><Name>Hickory</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>2</ID><Name>Soft Maple</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood><Wood><ID>3</ID><Name>Red Oak</Name><Weight>3</Weight><Thickness>4</Thickness><Density>5</Density><Purity>6</Purity><Age>7</Age></Wood></Woods>";
XDocument doc = XDocument.Parse(xml);
//Get your wood nodes and values in a list
List<Tuple<string,string>> list = doc.Descendants().Select(a=> new Tuple<string,string>(a.Name.LocalName,a.Value)).ToList();
// display the list
list.All(a => { Console.WriteLine(string.Format("Node name {0} , Node Value {1}", a.Item1, a.Item2)); return true; });
Console.Read();
}
}
您可以使用xmlDocument.SelectNodes("//child::node()")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.