c# XML Parsing将innerxml与innertext分离
我想要做的是理想情况下创建一个嵌套列表,基本上是一个二维列表,或者一个二维数组(如果这更适合此任务),其工作原理如下: 1 名称 => Hickory 无需显式选择节点。
我可以使用 SelectNode (Woods/Wood),然后执行类似 node["ID"].InnerText
的操作,但这需要我知道节点名称是什么。
假设即使有 36 个节点而不是 7 个,它也会读取 wood.xml
并且我永远不会知道节点的名称。我尝试使用 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();
}
如果你愿意的话,我基本上可以创建一个木质“缓冲区”,这样我就可以在其他地方访问这些值。
抱歉,如果我不清楚,我想本质上使这个“抽象”,因为缺乏更好的词。
因此,如果有一天我将“Weight”的名称更改为“HowHeavy”,或者添加一个附加元素“NumberOfBranches”,我就不必对 xml 文件的结构进行硬编码。
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.
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.
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. I tried using outerxml
/innerxml
but that gives me too much information.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是你所追求的吗?
Is this what you after ?
您可以使用 xmlDocument.SelectNodes("//child::node()")
You can use
xmlDocument.SelectNodes("//child::node()")