如何在XML C#中从根部遍历到其元素? [英] How can I traverse from the roots down to its elements in XML C#?

查看:55
本文介绍了如何在XML C#中从根部遍历到其元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<Device>
<pre><Information>
    <Model>Model name here</Model>
    <Manufacturer>Manufacturer name here</Manufacturer>
</Information>




<Output>
    <X>
        <Specifics>
            <VInput>10</VInput>
            <IInput>10</IInput>
            <VRes>0.10</<VRes>
            <IRes>0.10</IRes>
        </Specifics>
        
        <Instruction>
            <Id>"ID"</Id>
            <Values>PR0, DS0, VX{0}, AX{0}</Values>
            <MainSwitch>SW{0}</MainSwitch>
            <OutSwitch>OX{0}</OutSwitch>
        </Instruction>
    </X>
    <Y>
        <Specifics>
            <VInput>10</VInput>
            <IInput>10</IInput>
            <VRes>0.10</<VRes>
            <IRes>0.10</IRes>
        </Specifics>
        
        <Instruction>
            <Id>"ID"</Id>
            <Values>PR0, DS0, VY{0}, AY{0}</Values>
            <MainSwitch>SW{0}</MainSwitch>
            <OutSwitch>OY{0}</OutSwitch>
        </Instruction>
    </Y>
    <Z>
        <Specifics>
            <VInput>10</VInput>
            <IInput>10</IInput>
            <VRes>0.10</<VRes>
            <IRes>0.10</IRes>
        </Specifics>
        
        <Instruction>
            <Id>"ID"</Id>
            <Values>PR0, DS0, VZ{0}, AZ{0}</Values>
            <MainSwitch>SW{0}</MainSwitch>
            <OutSwitch>OZ{0}</OutSwitch>
        </Instruction>
    </Z>
</Output>







//如何在不手动搜索xml文件的情况下声明文件路径?

//如何从根遍历 - 设备 - 使用泛型类 - 获取PWID而不像示例代码那样手动声明它,因此我可以将其重用于其他。具有相同字段但值不同的xml文件。不使用标签信息,设置等



我尝试过:



string searchFile =C:\\Users\\XMLFiles \\PS.xml;

试试

{

XmlDocument document = new XmlDocument();

document.Load(searchFile);

XmlElement device = document [Device];

XmlElement输出=设备[输出];

XmlElement x =输出[X];

XmlElement指令= X [指令;;

字符串值= instruction.SelectSingleNode(Values)。InnerText;

Console.WriteLine(强制);

控制台。 ReadLine();

}



catch

{

throw;

}




//How can I search xml files without manually stating the filepath?
//How can I traverse from the root - Device - using a generic class - Get the PWID without manually stating it just like the sample code, so I can reuse it for other .xml files with same fields but different values. Without using the tags "information", "settings" and etc

What I have tried:

string searchFile = "C:\\Users\\XMLFiles\\PS.xml";
try
{
XmlDocument document = new XmlDocument();
document.Load(searchFile);
XmlElement device = document["Device"];
XmlElement output = device["Output"];
XmlElement x = output["X"];
XmlElement instruction = X["Instruction"];
string values = instruction.SelectSingleNode("Values").InnerText;
Console.WriteLine(force);
Console.ReadLine();
}

catch
{
throw;
}

推荐答案

请参见此处的示例: https://ehikioya.com/get-xml-document-nodes-recursively/ [ ^ ]

它使用:

See example here: https://ehikioya.com/get-xml-document-nodes-recursively/[^]
It uses:
XmlNodeList children = node.ChildNodes;


这篇关于如何在XML C#中从根部遍历到其元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆