如何使用C#从URL读取XML节点?

时间:2012-09-03 10:55:52

标签: c# xml url

我的节目如下。这个概念是来自URL的Read XML值,但我的程序只读取xml结构,而不是代码数据。仅限<Billing Address></Billing Address>...等。但原始XML值为<Billing Address>Strre1</Billing Address>。本程序未读取内部值。

public static void zohoCRMReadAccounts()
{

    var val = auth();
    var val1= val[0];
    var val2= val[1];

    String xmlURL = "URL";
    XmlTextReader xmlReader = new XmlTextReader(xmlURL);
    while (xmlReader.Read())
    {
        switch (xmlReader.NodeType)
        {
            case XmlNodeType.Element: // The node is an element.
                Console.Write("<" + xmlReader.Name);
                // Read the attributes:
                while (xmlReader.MoveToNextAttribute()) 
                    Console.Write(" " + xmlReader.Name + "=’" 
                                   + xmlReader.Value + "’");
                Console.WriteLine(">");
                break;
            case XmlNodeType.Text: //Display the text in each element.
                Console.WriteLine(xmlReader.Value);
                break;
            case XmlNodeType.EndElement: //Display the end of the element.
                Console.Write("</" + xmlReader.Name);
                Console.WriteLine(">");
                break;
        }
    }
    Console.WriteLine("Press any key to continue…");
    Console.ReadLine(); //Pause
}

请帮我解决

2 个答案:

答案 0 :(得分:1)

XML元素的名称中不能包含空格。尝试先删除它们

答案 1 :(得分:0)

首先下载XML。然后使用喜欢,

  try { 
    //read xml
    XmlDocument xdoc = new XmlDocument();
    xdoc.Load("XMLFilePath");
    XmlNodeList nodes = xdoc.SelectNodes(@"rss/channel/item");
    foreach (XmlNode node in nodes)
    {
      XmlNode titleNode = node.SelectSingleNode("title");
      string title = titleNode == null ? string.Empty : titleNode.InnerText;

      };

}