简体   繁体   English

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

[英]How to read XML node from URL using C#?

I'm having program like below. 我有下面的程序。 The concept is Read XML value from URL, but my program read the xml structure only, not the code datas. 这个概念是从URL读取XML值,但是我的程序仅读取xml结构,而不读取代码数据。 Like <Billing Address></Billing Address>... etc only. 仅像<Billing Address></Billing Address>...等。 But the original XML value is <Billing Address>Strre1</Billing Address> . 但是原始XML值为<Billing Address>Strre1</Billing Address> The Program does not read the inside value. 程序不读取内部值。

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
}

Please help me to fix 请帮我解决

XML Elements cannot have spaces in their names. XML元素的名称中不能包含空格。 Try to remove them first 尝试先删除它们

First download the XML. 首先下载XML。 Then Use like, 然后像这样使用

  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;

      };

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM