首页 > 解决方案 > 无法解析和读取 xml 数据

问题描述

我有如下的xml格式。并试图从内容、产品名称和产品 ID 中读取元素但无法读取。这是我迄今为止尝试过的,但没有运气。我的两种方法都不起作用,感谢您的帮助。

<source xml:base="https://google.com/api/v1" xmlns="http://www.w3.org/2005/Atom" > 
  <id>s1</id>  
  <value>
    <id>value1</id>
    <version>1.90</version>
        <content type="application/xml">
            <x:products>
                <n:Productname>3M</n:Productname>
                <n:ProductId n:type="Int32">97</n:ProductId>       
            </x:products>
            <x:products>
                <n:Productname>HD</n:Productname>
                <n:ProductId n:type="Int32">99</n:ProductId>       
            </x:products>
        </content>
  </value>
</source>

 FileStream fs = new FileStream(xmlFile, FileMode.Open, FileAccess.Read);
            XmlDocument xmldoc = new XmlDocument();           
            XmlNodeList xmlnodecontent;

            xmldoc.Load(fs);           

            xmlnodecontent = xmldoc.GetElementsByTagName("content");
            for (int i = 0; i < xmlnodecontent.Count; i++)
            {
                var innerXml =xmlnodecontent[i].ChildNodes.Item(0).InnerXml;
                //Trying to read product here
            }

 //Second approach

      var doc = XDocument.Load(xmlFile);

      var units = from u in doc.Descendants("value")
                        select new
                        {
                            Id = (int)u.Element("id"),
                            content = from entry in doc.Descendants("content")
                                      select new
                                      {
                                          product = (int)u.Element("d:Product"),
                                      }
                        };
            foreach (var unit in units)
            {
                var id = unit.Id;
                var content = unit.content;
            }

标签: c#xmllinqxml-parsing

解决方案


我更正了 xml 文件并使用 xml linq (XDocument) 来获取值

<source xml:base="https://google.com/api/v1" xmlns="http://www.w3.org/2005/Atom" xmlns:x="abc" xmlns:n="def" >
  <id>s1</id>
  <value>
    <id>value1</id>
    <version>1.90</version>
    <content type="application/xml">
      <x:products>
        <n:Productname>3M</n:Productname>
        <n:ProductId n:type="Int32">97</n:ProductId>
      </x:products>
      <x:products>
        <n:Productname>HD</n:Productname>
        <n:ProductId n:type="Int32">99</n:ProductId>
      </x:products>
    </content>
  </value>
</source>

这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement value = doc.Descendants().Where(x => x.Name.LocalName == "value").FirstOrDefault();
            XNamespace ns = value.GetDefaultNamespace();
            XNamespace xNs = value.GetNamespaceOfPrefix("x");
            XNamespace nNs = value.GetNamespaceOfPrefix("n");

            var values = doc.Descendants(ns + "value").Select(x => new
            {
                id = (string)x.Element(ns + "id"),
                products = x.Descendants(xNs + "products").Select(y => new
                {
                    name = (string)y.Element(nNs + "Productname"),
                    id = (string)y.Element(nNs + "ProductId")
                }).ToList()
            }).ToList();
        }
    }
}

推荐阅读