【问题标题】:Read XML with schema without xsd file使用没有 xsd 文件的架构读取 XML
【发布时间】:2013-07-22 15:09:32
【问题描述】:

这周我收到了一个复杂的 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>

这只是真实文件的一个样本,使用了假数据。

有没有办法在这个节点上做一个 foreach 来找到每个日期的 FinalValue?

【问题讨论】:

    标签: c# xml xml-serialization xmlreader xmlnodelist


    【解决方案1】:

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

    读取 XML 文件。我建议使用 Linq-to-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();
    

    更新

    要返回DateFinalValue,您可以使用匿名类型:

    const string mpNamespace = "bs.TestParameters";
    const string xcNamespace = "XmlTest";
    
    XDocument xDocument = XDocument.Load("C:/Path to the file.xml");
    
    var 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 new  // Creates an instance of an anonymous type with Date and FinalValue
                       {
                           Date = dateNode.Attribute(XName.Get("Value", xcNamespace)).Value,
                           FinalValue = finalValueNode.Value  // Gets the value of each FinalValue node
                       }
                      ).ToList();
    

    【讨论】:

    • 它看起来是一个不错的答案,但我如何选择日期和 FinalValue?
    • @raddesso 我已经更新了我的答案。返回DateFinalValue
    • 非常感谢!我会做一些测试,但我想它会起作用的!
    【解决方案2】:
    XmlDocument doc = new XmlDocument();
    doc.Load("path to xml file");
    
    XmlNodeList finalValues = doc.GetElementByTagName("FinalValue");
    

    finalValues 将是具有标签名称“FinalValue”的节点列表,然后您可以读取列表中的内部文本和故事。

    List<string> values = new List<string>();
    foreach(XmlNode node in finalValues)
        values.Add(node.InnerText);
    

    【讨论】:

      猜你喜欢
      • 2020-11-15
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 2020-06-25
      • 1970-01-01
      相关资源
      最近更新 更多