Results 1 to 6 of 6
  1. #1

    Default HELP!!!!! c# and XML


    Naay koy xml file sa mga machine settings

    <machines>
    <machine>
    <name> pc1 </name>
    <username> user1 </username>
    <password> password1 </password>
    </machine>
    <machine>
    <name> pc2 </name>
    <username> user2 </username>
    <password> password2 </password>
    </machine>
    <machine>
    <name> pc3 </name>
    <username> user3 </username>
    <password> password3 </password>
    </machine>
    </machines>


    unsaun ni pagstore into memory? para ka-isa ra ko mu read sa xml file. Then ang manipulations (add, edit, delete) kay in memory ra buhatun not sa xml.



    Istoryans, I need your programming expertise. What's the best way to do it?

  2. #2
    Gamit ug XMLDocument class para in memory manipulation.

  3. #3
    complicated raman au na sir.

    try nalang nako array of classes.

  4. #4
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    
    namespace XmlDocumentExample
    {
        class Program
        {
            static void Main(string[] args)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("C:\\test.xml");
                //pagmaya sa xml, CRUD attributes/nodes ... etc
                doc.Save("newfilename.xml");
            }
        }
    }
    Complicated?

  5. #5
    ang manipulations ang lisuran nako.
    naa pa cya inner text, outer text, child etc..

    kita ko xml.linq. mas sayun xa
    thanks anyway.

  6. #6
    hi this structure can be represented in a List of class.

    Code:
    List<machine> machines = new List<machine>(); // you save/load this to file
    
    class machine{
       public string name
       public string user2
       public string password2
    }
    if you have the entire xml you can represent it into a class.

    after that use xml serialization to save and load that object.

    Code:
    public static object Load(string path, object destinationType)
    {
        if (!File.Exists(path))
        {
            throw new Exception("File not found! " + path);
        }
        string pXmlString = File.ReadAllText(path);
        XmlSerializer serializer = new XmlSerializer(destinationType.GetType());
        MemoryStream stream = new MemoryStream(StringToUTF8ByteArray(pXmlString));
        return serializer.Deserialize(stream);
    }
    
    public static void Save(string filename, object obj)
    {
        try
        {
            string fullPath = Path.GetFullPath(filename);
            if (!Directory.Exists(Path.GetDirectoryName(fullPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
            }
            string str2 = null;
            MemoryStream w = new MemoryStream();
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            XmlTextWriter writer = new XmlTextWriter(w, Encoding.UTF8);
            writer.Formatting = Formatting.None;
            serializer.Serialize((XmlWriter) writer, obj);
            str2 = UTF8ByteArrayToString(((MemoryStream) writer.BaseStream).ToArray());
            File.WriteAllText(fullPath, str2);
        }
        catch (Exception)
        {
            throw;
        }
    }
    
    private static byte[] StringToUTF8ByteArray(string pXmlString)
    {
        UTF8Encoding encoding = new UTF8Encoding();
        return encoding.GetBytes(pXmlString);
    }
    
     
    private static string UTF8ByteArrayToString(byte[] characters)
    {
        UTF8Encoding encoding = new UTF8Encoding();
        return encoding.GetString(characters);
    }

  7.    Advertisement

Similar Threads

 
  1. NID HELP... RAM and video card for RF...
    By padzie in forum Computer Hardware
    Replies: 4
    Last Post: 03-16-2008, 01:33 PM
  2. help.. pc400 and pc 533 ddr, puyd dunganon gamit?
    By gamhanan in forum Computer Hardware
    Replies: 7
    Last Post: 06-13-2007, 04:37 PM
  3. Help - - Distorted and Scratchy Sound
    By rochelle in forum Computer Hardware
    Replies: 8
    Last Post: 08-07-2006, 09:28 PM
  4. Help! VB and Firebird.
    By daryl in forum Programming
    Replies: 1
    Last Post: 05-11-2006, 11:01 PM
  5. HELP: Type and MaxSize of HD (Asus P2B98-XV and Intel CA810)
    By rocknolds in forum Computer Hardware
    Replies: 4
    Last Post: 11-15-2005, 04:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top