简体   繁体   中英

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. Like <Billing Address></Billing Address>... etc only. But the original XML value is <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. Try to remove them first

First download the 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;

      };

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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