SIMIN Posted October 17, 2008 Posted October 17, 2008 hello all, This is my first time I wanna read from a XML file! I have a XML file like this: <?xml version="1.0" encoding="utf-16" ?> <MessageAccount> <Account_Name type="SZ">my account #1</Account_Name> <Connection_Type type="DWORD">00000003</Connection_Type> <POP3_Server type="SZ">pop.domain.com</POP3_Server> <POP3_User_Name type="SZ">info</POP3_User_Name> <POP3_Password2 type="BINARY">gfdjkgdfghdfjkghdfgjk</POP3_Password2> <POP3_Use_Sicily type="DWORD">00000000</POP3_Use_Sicily> <POP3_Prompt_for_Password type="DWORD">00000000</POP3_Prompt_for_Password> <SMTP_Server type="SZ">smtp.domain.com</SMTP_Server> <SMTP_Display_Name type="SZ">My Name</SMTP_Display_Name> <SMTP_Email_Address type="SZ">info@domain.com</SMTP_Email_Address> </MessageAccount> Well, I know I should use XMLReader for this: Dim reader As XmlTextReader = New XmlTextReader (URLString) Also, I know I can read it in a loop: Do While (reader.Read()) Console.WriteLine(reader.Name) Loop But the data are returned in a strange sort! Even if I split the reader.NodeType. However, I am new to this. I just wanna pass the TEXT and get the VALUE. And don't know how. Please help me. For example in this XML file: <Account_Name type="SZ">my account #1</Account_Name> I wanna pass Account_Name and get my account #1. ... I searched and worked a lot but could not find it. Please help me, Thank you. Quote
Nate Bross Posted October 17, 2008 Posted October 17, 2008 If you are using .NET 3.5 you could use LINQ to XML. Scott Gu has a good example here. If using prior version of .NET, this article may be worth a read. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
SIMIN Posted October 17, 2008 Author Posted October 17, 2008 Hello and thank you very much for helping me :) As a matter of fact, I already red that article (I use .NET Framework 2) I also played around with all these for around 24 hours! But could not find how to read it like this: If this is our row: <Account_Name type="SZ">my account #1</Account_Name> I wanna get the value of Account_Name, which is my account #1 How this can be done? :) Thank you Quote
Diesel Posted October 17, 2008 Posted October 17, 2008 There are a good number of ways to read xml with the .Net classes...and here is a clean, fast way. namespace XmlRead { using System; using System.Xml; class Program { static void Main(string[] args) { XmlReader reader = XmlReader.Create("data.xml"); while (reader.Read()) { if (reader.NodeType != XmlNodeType.Element) reader.MoveToContent(); if (reader.NodeType == XmlNodeType.EndElement) { reader.Read(); continue; } switch (reader.Name.ToLower()) { case "account_name": Console.WriteLine("account_name: " + reader.ReadString()); continue; } reader.Read(); } Console.ReadLine(); reader.Close(); } } } [/Code] Also, the recommend naming convention for XML tags is lower camel case, with no special characters, ie . hyphens. Quote
SIMIN Posted October 17, 2008 Author Posted October 17, 2008 Thank you Diesel, It was a great help and will work on it to see what you have done, so I learn it :p Anyway, just one question about your code: You have While reader.Read() . So is it necessary to have reader.Read() Before End While? Quote
Diesel Posted October 17, 2008 Posted October 17, 2008 It depends on how you set up the code inside the loop to read the xml, and if you do a priming read. Here, the while(reader.Read()) statement serves as the priming read and the iteration read. What I mean by the priming read is that before the loop, the xml stream pointer points to nothing and the first read statement serves to move the pointer to something, usually the xml declaration. What I mean by the iteration read is that once an element is read...or not read, it increments the stream pointer. Actually, in this case, you can probably take out the reader.Read() at the bottom of the loop. That serves to increment the pointer if the element is not processed, which the while statement does. An alternative to while (reader.Read()) is to use a boolean variable and set it's value everytime reader.Read() is called inside the loop, ie. flag = reader.Read(); That way you can terminate the loop on any condition you choose. I prefer the flag. For completeness you can also add the default switch condition with just a break; body. Quote
SIMIN Posted October 17, 2008 Author Posted October 17, 2008 Hello again, :) You are great :) Thank you, I've learned a lot from you. Just one think I cannot find out is that in my real data: <?xml version="1.0" encoding="utf-16" ?> <MessageAccount> <Account_Name type="SZ">my account #1</Account_Name> <Connection_Type type="DWORD">00000003</Connection_Type> <POP3_Server type="SZ">pop.domain.com</POP3_Server> <POP3_User_Name type="SZ">info</POP3_User_Name> <POP3_Password2 type="BINARY">gfdjkgdfghdfjkghdfgjk</POP3_Password2> <POP3_Use_Sicily type="DWORD">00000000</POP3_Use_Sicily> <POP3_Prompt_for_Password type="DWORD">00000000</POP3_Prompt_for_Password> <SMTP_Server type="SZ">smtp.domain.com</SMTP_Server> <SMTP_Display_Name type="SZ">My Name</SMTP_Display_Name> <SMTP_Email_Address type="SZ">info@domain.com</SMTP_Email_Address> </MessageAccount> Some fields are type="SZ", or string. While some others are in this format: <SMTP_Port type="DWORD">00000019</SMTP_Port> I am not sure how is this. But I think it's in Hexadecimal format?!!! :confused: Which I should convert to Decimal format to get the real value? In this sample, SMTP port is definitely 25! Do you have any idea how can I do this ? :) Thank you again for your help. :p Quote
Diesel Posted October 17, 2008 Posted October 17, 2008 case "smtp_port": { int tmp; if (int.TryParse(data.ReadString(), NumberStyles.HexNumber, CultureInfo.CurrentCulture, out tmp)) { //property = tmp; } continue; } [/Code] DWORD stands for double word, or a byte. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.