Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用XML从元素中获取值_C#_Xml_Linq To Xml - Fatal编程技术网

C# 使用XML从元素中获取值

C# 使用XML从元素中获取值,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,我可以从XML中获取标题属性,但我想提取标题,它位于下面几个节点。我在代码中留下了我的最新尝试。 FirstAttributes工作,所以我知道我正在连接,如果我返回Console.WriteLine(e),我将获得完整的XML var url = "http://musicbrainz.org/ws/2/release-group/?query=artist:%22coldplay%22%20AND%20primarytype:%22single%22"; HttpWebRequest re

我可以从XML中获取标题属性,但我想提取标题,它位于下面几个节点。我在代码中留下了我的最新尝试。 FirstAttributes工作,所以我知道我正在连接,如果我返回Console.WriteLine(e),我将获得完整的XML

var url = "http://musicbrainz.org/ws/2/release-group/?query=artist:%22coldplay%22%20AND%20primarytype:%22single%22";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "Hello World Super Script";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
XDocument doc = XDocument.Load(response.GetResponseStream());

IEnumerable<XElement> childList =
from el in doc.Elements()  
select el; 
//title is element we need  
foreach (XElement e in childList)  
Console.WriteLine("{0} {1} {2}", e.FirstAttribute, e.FirstAttribute.NextAttribute, e.Element("release-group").Attribute("title")); 
var url=”http://musicbrainz.org/ws/2/release-group/?query=artist:%22coldplay%22%20AND%20primarytype:%22single%22";
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(url);
request.UserAgent=“Hello World Super Script”;
HttpWebResponse=(HttpWebResponse)request.GetResponse();
XDocument doc=XDocument.Load(response.GetResponseStream());
IEnumerable子列表=
来自文档元素()中的el
选择el;
//标题是我们需要的元素
foreach(子列表中的元素e)
Console.WriteLine(“{0}{1}{2}”、e.FirstAttribute、e.FirstAttribute.NextAttribute、e.Element(“发布组”).Attribute(“标题”);

研究:

您可以执行以下操作

var doc = XDocument.Load(response.GetResponseStream());
XNamespace ns = "http://musicbrainz.org/ns/mmd-2.0#";

var titleList = doc.Descendants(ns + "title");
foreach (var element in titleList) 
    Console.WriteLine(element.Value);
XDocument.subjections()
允许您搜索具有指定名称的子节点。请注意,您需要指定名称空间和元素名称

如果您观察来自WebRequest的响应,您可以找到名称空间的详细信息

xmlns="http://musicbrainz.org/ns/mmd-2.0#" 

您可以执行以下操作

var doc = XDocument.Load(response.GetResponseStream());
XNamespace ns = "http://musicbrainz.org/ns/mmd-2.0#";

var titleList = doc.Descendants(ns + "title");
foreach (var element in titleList) 
    Console.WriteLine(element.Value);
XDocument.subjections()
允许您搜索具有指定名称的子节点。请注意,您需要指定名称空间和元素名称

如果您观察来自WebRequest的响应,您可以找到名称空间的详细信息

xmlns="http://musicbrainz.org/ns/mmd-2.0#" 

尝试以下操作:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string URL = @"http://musicbrainz.org/ws/2/release-group/?query=artist:%22coldplay%22%20AND%20primarytype:%22single%22";
        static void Main(string[] args)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.UserAgent = "Hello World Super Script";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            XDocument doc = XDocument.Load(response.GetResponseStream());
            XNamespace ns = doc.Root.GetDefaultNamespace();

            List<Group> groups = doc.Descendants(ns + "release-group").Select(x => new Group()
            {
                title = (string)x.Element(ns + "title"),
                name = (string)x.Descendants(ns + "name").FirstOrDefault(),
                releaseTitles = x.Element(ns + "release-list").Descendants(ns + "title").Select(y => (string)y).ToArray()
            }).ToList();
        }
    }
    public class Group
    {
        public string title { get; set; }
        public string name { get; set; }
        public string[] releaseTitles { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
Net系统;
命名空间控制台应用程序1
{
班级计划
{
常量字符串URL=@“http://musicbrainz.org/ws/2/release-group/?query=artist:%22coldplay%22%20AND%20primarytype:%22single%22";
静态void Main(字符串[]参数)
{
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(URL);
request.UserAgent=“Hello World Super Script”;
HttpWebResponse=(HttpWebResponse)request.GetResponse();
XDocument doc=XDocument.Load(response.GetResponseStream());
XNamespace ns=doc.Root.GetDefaultNamespace();
列表组=文档子体(ns+“发布组”)。选择(x=>新组()
{
title=(字符串)x.Element(ns+“title”),
name=(字符串)x.subjects(ns+“name”).FirstOrDefault(),
releaseTitles=x.Element(ns+“release list”)。子体(ns+“title”)。选择(y=>(string)y)。ToArray()
}).ToList();
}
}
公共课组
{
公共字符串标题{get;set;}
公共字符串名称{get;set;}
公共字符串[]releaseTitles{get;set;}
}
}
尝试以下操作:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string URL = @"http://musicbrainz.org/ws/2/release-group/?query=artist:%22coldplay%22%20AND%20primarytype:%22single%22";
        static void Main(string[] args)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.UserAgent = "Hello World Super Script";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            XDocument doc = XDocument.Load(response.GetResponseStream());
            XNamespace ns = doc.Root.GetDefaultNamespace();

            List<Group> groups = doc.Descendants(ns + "release-group").Select(x => new Group()
            {
                title = (string)x.Element(ns + "title"),
                name = (string)x.Descendants(ns + "name").FirstOrDefault(),
                releaseTitles = x.Element(ns + "release-list").Descendants(ns + "title").Select(y => (string)y).ToArray()
            }).ToList();
        }
    }
    public class Group
    {
        public string title { get; set; }
        public string name { get; set; }
        public string[] releaseTitles { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
Net系统;
命名空间控制台应用程序1
{
班级计划
{
常量字符串URL=@“http://musicbrainz.org/ws/2/release-group/?query=artist:%22coldplay%22%20AND%20primarytype:%22single%22";
静态void Main(字符串[]参数)
{
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(URL);
request.UserAgent=“Hello World Super Script”;
HttpWebResponse=(HttpWebResponse)request.GetResponse();
XDocument doc=XDocument.Load(response.GetResponseStream());
XNamespace ns=doc.Root.GetDefaultNamespace();
列表组=文档子体(ns+“发布组”)。选择(x=>新组()
{
title=(字符串)x.Element(ns+“title”),
name=(字符串)x.subjects(ns+“name”).FirstOrDefault(),
releaseTitles=x.Element(ns+“release list”)。子体(ns+“title”)。选择(y=>(string)y)。ToArray()
}).ToList();
}
}
公共课组
{
公共字符串标题{get;set;}
公共字符串名称{get;set;}
公共字符串[]releaseTitles{get;set;}
}
}

能否添加Xml元素示例和预期输出?能否添加Xml元素示例和预期输出?