Skip to main content

#XML #C# Read a XML (from a string) and get some fields


string myXML = "<?xml version=\"1.0\" encoding=\"utf-16\"?><myDataz xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><listS> <sog><field1>123</field1><field2>a</field2><field3>b</field3> </sog><sog><field1>456</field1><field2>c</field2><field3>d</field3> </sog></listS></myDataz>" ;

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
string xpath = "myDataz/listS/sog";
var nodes = xmlDoc.SelectNodes(xpath);
foreach (XmlNode childrenNode in nodes)
{
     HttpContext.Current.Response.Write(childrenNode.SelectSingleNode("//field1").Value);
}

Try :
        childrenNode.SelectSingleNode("field1").Value
.InnerText or .InnerXML

-------------------------------------------------
string decryptedMarkup = "<Product><Type>Metal</Type>"
    + "<Department>Foundry</Department></Product>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(decryptedMarkup);


// XmlDocument.Load(string)

string pathToFile = "test.xml";
XmlDocument xmlDoc2 = new XmlDocument();
xmlDoc2.Load(pathToFile);
--------------------------------------------
For further information, take a look at: XmlDocument.Load(string)
String parameter: URL for the file containing the XML document to load. The URL can be either a local file or an HTTP URL (a Web address).
String parameter: String containing the XML document to load.
Source :
"<?xml version=\"1.0\" encoding=\"utf-16\"?>
<myDataz xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
<listS>

<sog>
<field1>123</field1>
<field2>a</field2>
<field3>b</field3>
</sog>

<sog>
<field1>456</field1>
<field2>c</field2>
<field3>d</field3>
</sog>

</listS>
</myDataz>"


Ref : https://stackoverflow.com/questions/8401280/read-a-xml-from-a-string-and-get-some-fields-problems-reading-xml
 

Comments

Popular posts from this blog

Use Log4Net in C# windows form Application

we are going to learn on how to use the Log4Net library for creating logs. Create a new windows form application in VS. Install Log4Net library Add to AssemblyInfo.cs  Configure in  App.config Use in code  Install Log4Net library Then install the Log4Net library from the Nuget Package library.           log4net by The Apache software foundation 2.0.8 (i installed the latest). Add to AssemblyInfo.cs  After installing this package, open up AssemblyInfo .cs file under the Properties folder and add the log4net assembly information into it (under the other assembly information.).    [assembly: log4net.Config.XmlConfigurator(Watch= true )]  Configure in  App.config Now, open the App.config file and enter required details for LogNet to work. <configSections>       <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2....

#SQL: How to Drop Database in SQL Server by Closing Existing Connections

How to Drop Database in SQL Server when the users are connected to a SQL Server Database. You may need to closing existing connections in a database before restoring the database, before detaching database, get database in SINGLE_USER mode etc. If you try dropping a database when users are connected to the SQL Server Database you will receive the below mentioned error message. Error Message Drop failed for Database 'RajDB'. (Microsoft.SqlServer.Smo) Cannot drop database "" because it is currently in use. (Microsoft SQL Server, Error: 3702) USE [master] GO ALTER DATABASE RajDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE GO /* Query to Drop Database in SQL Server  */ DROP DATABASE RajDB GO