how to load xml file in c# - c#

Can any one give me a proper explain why i am unable to get updated XML content from URL. I have a XML file which will frequently update. But in my application i am getting old data. Until i restart my application.
Here i am placing my code that i have tried
XmlDocument doc = new XmlDocument();;
string str;
using (var wc = new WebClient())
{
str = wc.DownloadString(location.AbsoluteUri);
}
doc.LoadXml(str);
And also tried with below code
WebRequest req = HttpWebRequest.Create("url");
using (Stream stream = req.GetResponse().GetResponseStream())
{
xmldoc.Load(stream);
}

I got to know that raw git hub take time to update in all servers so it is taking time to update. So you can use other web services to get result you want.

Related

Trying to download string and can't search in it context

I am using WebClient to download string html from WebSite and then i am trying to manipulate the string by using SubString and IndexOf..
Also some times i use the functions: substring, indexOf or contains and a strange thing happens:
Some times it shows a text (HTML code) and some times it isn't show anything at all.
using (WebClient client = new WebClient())
{
htmlCode = client.DownloadString("https://www.google.com");
}
This is my code for getting an html code from a web site.
Now for example in this site i want to get the source of an image - a specific img (or another attribute)
using (StringReader reader = new StringReader(htmlCode))
{
string inputLine;
while ((inputLine = reader.ReadLine()) != null)
{
if (inputLine.Contains("img"))
{
RichTextBox.Text += inputLine;
}
}
}
There May be some syntax problems but don't look at it, They are not important.
Do you have an alterenetive or better way to get an HTML source code from a page and handle with it. It has to be HTTPS site and i would like a good explanation of it.
Sorry for noob question.

C# Read ini value from url

I have a site for example "http://example.com" and here is settings.ini file "http://example.com/settings.ini" , in file wrriten this text:
[Client]
Enabled=1
I wan't to give that value from C#, is it possible, how to?
For example I'm using this code:
var MyIni = new IniFile(#"C:\settings.ini");
var DefaultVolume = MyIni.Read("Enabled");
MessageBox.Show(DefaultVolume);
It works good, I'm trying to make something like this:
var MyIni = new IniFile(#"http://example.com/settings.ini");
but its not work, thanks.
EDIT:
I'm getting this error:
An unhandled exception of type "System.ArgumentException" in mscorlib.dll
For more information: URI formats are not supported.
Updated:
This code gets all value from .ini, now i need to integrate to my old code
WebClient client = new WebClient();
Stream stream = client.OpenRead("http://example.com/settings.ini");
StreamReader reader = new StreamReader(stream);
String content = reader.ReadToEnd();
Read ini file content with HttpWebRequest (http://www.csharp-station.com/HowTo/HttpWebFetch.aspx)
You can use my library in order to retrieve your settings from an INI file:
https://github.com/MarioZ/MadMilkman.Ini
For example like the following:
WebClient client = new WebClient();
IniFile myIni = new IniFile();
myIni.Load(client.OpenRead("http://example.com/settings.ini"));
string defaultVolume = myIni.Sections["Client"].Keys["Enabled"].Value;
MessageBox.Show(defaultVolume);
Also just as an FYI, you can retrieve that value as an integer like the following:
int volume;
myIni.Sections["Client"].Keys["Enabled"].TryParseValue(out volume);
I hope this helps.

Saving File on Desktop by c#

I am using a web service that returns me some data. I am writing that data in a text file. my problem is that I am having a file already specified in the c# code, where I want to open a dialog box which ask user to save file in his desired location. Here I am posting code which I have used. Please help me in modifying my code. Actually after searching from internet, all are having different views and there is lot of changes in code required where as I do not want to change my code in extent. I am able to write the content in test file but how can I ask user to enter his desire location on computer?
StreamWriter file = new StreamWriter("D:\\test.txt");
HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(yahooURL);
// Get the response from the Internet resource.
HttpWebResponse webresp = (HttpWebResponse)webreq.GetResponse();
// Read the body of the response from the server.
StreamReader strm =
new StreamReader(webresp.GetResponseStream(), Encoding.ASCII);
string content = "";
for (int i = 0; i < symbols.Length; i++)
{
// Loop through each line from the stream,
// building the return XML Document string
if (symbols[i].Trim() == "")
continue;
content = strm.ReadLine().Replace("\"", "");
string[] contents = content.ToString().Split(',');
foreach (string dataToWrite in contents)
{
file.WriteLine(dataToWrite);
}
}
file.Close();
Try this
using (WebClient Client = new WebClient ())
{
Client.DownloadFile("http://www.abc.com/file/song/a.mpeg", "a.mpeg");
}

How to get xml file from server in OOB Application using web client method

I am using Out of Browser Application in silverlight.
I have problem while loading xml file usine below mentioned code.
string contentUri = Application.Current.Host.Source
.AbsoluteUri;
var contentUri1 = contentUri.Substring(0, contentUri.LastIndexOf("/")) + "/Hello1.xml";
WebClient wc = new WebClient();
wc.OpenReadCompleted+=(open,read)=>
{
string content;
using (StreamReader reader = new StreamReader(read.Result,Encoding.Unicode))
{
byte[] m_Bytes = ReadToEnd(read.Result);
string s = Encoding.UTF8.GetString(m_Bytes, 0, m_Bytes.Length);
}
};
wc.DownloadProgressChanged
+= (chang,dh)=>
{
};
wc.OpenReadAsync(new Uri(contentUri));
where my xml file contained
<Root>
<element>FirstElement</element>
</Root>
I got the garbage value as output can anyone please help me how can i download that original xml content?
When you invoke webclient calls on Out-Of-Browser mode it suppose that you have already RootVisual Created, because webclient will run on its Dispatcher apparently.
If not, you end up with no response from server, and what is strange, even exception is not thrown !!
Anyway, this Post from Jeremy explains details:
http://csharperimage.jeremylikness.com/2010/05/webclient-and-deploymentcatalog-gotchas.html

How to open txt file on localhost and change is content

i want to open a css file using C# 4.5 and change only one file at a time.
Doing it like this gives me the exception - URI formats are not supported.
What is the most effective way to do it ?
Can I find the line and replace it without reading the whole file ?
Can the line that I am looking and than start to insert text until
cursor is pointing on some char ?
public void ChangeColor()
{
string text = File.ReadAllText("http://localhost:8080/game/Css/style.css");
text = text.Replace("class='replace'", "new value");
File.WriteAllText("D://p.htm", text);
}
I believe File.ReadAllText is expecting a file path, not a URL.
No, you cannot search/replace sections of a text file without reading and re-writing the whole file. It's just a text file, not a database.
most effective way to do it is to declare any control you want to alter the css of as "runat=server" and then modify the CssClass property of it. There is no known alternative way to modify the css file directly. Any other hacks is just that.. a hack and very innefficient way to do it.
As mentioned before File.ReadAllText does not support url. Following is a working example with WebRequest:
{
Uri uri = new Uri("http://localhost:8080/game/Css/style.css");
WebRequest req = WebRequest.Create(uri);
WebResponse web = req.GetResponse();
Stream stream = web.GetResponseStream();
string content = string.Empty;
using (StreamReader sr = new StreamReader(stream))
{
content = sr.ReadToEnd();
}
content.Replace("class='replace'", "new value");
using (StreamWriter sw = new StreamWriter("D://p.htm"))
{
sw.Write(content);
sw.Flush();
}
}

Categories

Resources