C# 读取不带xsd文件的模式的XML

C# 读取不带xsd文件的模式的XML,c#,xml,xml-serialization,xmlreader,xmlnodelist,C#,Xml,Xml Serialization,Xmlreader,Xmlnodelist,本周我收到了一个复杂的XML文件,它是基于模式的,但我没有收到任何xsd文件,我需要读取其中的每个节点 XML示例如下: <xc:XmlTest xmlns:xc="XmlTest" xmlns:mp="bs.TestParameters" xmlns:rt="bs.TestParameters.Parameter1" xmlns:rtcu="bs.TestParameters.Parameter1.Var"> <xc:XmlTestArea xc:value="TestPar

本周我收到了一个复杂的XML文件,它是基于模式的,但我没有收到任何xsd文件,我需要读取其中的每个节点

XML示例如下:

<xc:XmlTest xmlns:xc="XmlTest" xmlns:mp="bs.TestParameters" xmlns:rt="bs.TestParameters.Parameter1" xmlns:rtcu="bs.TestParameters.Parameter1.Var">
<xc:XmlTestArea xc:value="TestParameters">
    <mp:Name xc:Value="raddesso" xmlns:mp="bs.TestParameters">
        <mp:Date xc:Value="20130215">
            <rt:RunTest xmlns:rt="bs.TestParameters.Parameter1">
                <rtcu:Var xmlns:rtcu="bs.TestParameters.Parameter1.Var">
                    <mp:FinalValue>1234</mp:FinalValue>
                </rtcu:Var>
            </rt:RunTest>
        </mp:Date>
        <mp:Date xc:Value="20130216">
            <rt:RunTest xmlns:rt="bs.TestParameters.Parameter1">
                <rtcu:Var xmlns:rtcu="bs.TestParameters.Parameter1.Var">
                    <mp:FinalValue>23234</mp:FinalValue>
                </rtcu:Var>
            </rt:RunTest>
        </mp:Date>
    </mp:Name>
</xc:XmlTestArea>
</xc:XmlTest>

1234
23234
这只是使用假数据的真实文件的一个示例


是否有任何方法可以在此节点上执行foreach以查找每个日期的最终值?

读取文件不需要架构。架构仅用于验证文件(检查完整性)。但这一步是可选的

读取XML文件。我建议使用Linq转换XML

const string mpNamespace = "bs.TestParameters";

XDocument xDocument = XDocument.Load("C:/Path to the file.xml");

List<string> finalValues = (from dateNode in xDocument.Descendants(XName.Get("Date", mpNamespace))  // Gets all Date nodes in the document
                            from finalValueNode in dateNode.Descendants(XName.Get("FinalValue", mpNamespace))  // Gets all FinalValue nodes in the Date nodes
                            select finalValueNode.Value  // Gets the value of each FinalValue node
                           ).ToList();

读取文件不需要架构。架构仅用于验证文件(检查完整性)。但这一步是可选的

读取XML文件。我建议使用Linq转换XML

const string mpNamespace = "bs.TestParameters";

XDocument xDocument = XDocument.Load("C:/Path to the file.xml");

List<string> finalValues = (from dateNode in xDocument.Descendants(XName.Get("Date", mpNamespace))  // Gets all Date nodes in the document
                            from finalValueNode in dateNode.Descendants(XName.Get("FinalValue", mpNamespace))  // Gets all FinalValue nodes in the Date nodes
                            select finalValueNode.Value  // Gets the value of each FinalValue node
                           ).ToList();
finalValues
将是一个具有标记名“FinalValue”的节点列表,然后您可以阅读列表中的内部文本和故事

List<string> values = new List<string>();
foreach(XmlNode node in finalValues)
    values.Add(node.InnerText);
列表值=新列表();
foreach(FinalValue中的XmlNode节点)
添加(node.InnerText);
finalValues
将是一个具有标记名“FinalValue”的节点列表,然后您可以阅读列表中的内部文本和故事

List<string> values = new List<string>();
foreach(XmlNode node in finalValues)
    values.Add(node.InnerText);
列表值=新列表();
foreach(FinalValue中的XmlNode节点)
添加(node.InnerText);

这个答案看起来不错,但我如何选择日期和最终值呢?@raddesso我已经更新了答案。返回
日期
最终值
。非常感谢!我会做一些测试,但我想会成功的!这看起来是个不错的答案,但是我如何选择日期和最终值呢?@raddesso我已经更新了我的答案。返回
日期
最终值
。非常感谢!我会做一些测试,但我想会成功的!