Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

XML question ...

hosssein007hosssein007 Posts: 3Member
I need to store some Key/Value pairs associated with a value(id) in a XML file.The xml file would have the following structure :


value
value
value
.
.
.



value
value
value
.
.
.

.
.
.
My question is How I can save and retrieve information to and from the document.Show me the full code please.
Because I may have more than one xml file of this type that each store different key/value pairs,I need a class that encapsulate everything.I mean it takes as an argument the file name and language "id" and then retrieve all key/value pairs associated with that "id" and we can access them using the class properties.Show me the attributes,methods... the class should have,the data types the class should use.(e.g. IEnumerable ...)

I need to use this in a multi-language website for storing static contents for different languages. I need it soon. Help me please !

Thanks

Comments

  • amigo77amigo77 Posts: 10Member
    : I need to store some Key/Value pairs associated with a value(id) in a XML file.The xml file would have the following structure :
    :
    :
    : value
    : value
    : value
    : .
    : .
    : .
    :
    :
    :
    : value
    : value
    : value
    : .
    : .
    : .
    :
    : .
    : .
    : .
    : My question is How I can save and retrieve information to and from the document.Show me the full code please.
    : Because I may have more than one xml file of this type that each store different key/value pairs,I need a class that encapsulate everything.I mean it takes as an argument the file name and language "id" and then retrieve all key/value pairs associated with that "id" and we can access them using the class properties.Show me the attributes,methods... the class should have,the data types the class should use.(e.g. IEnumerable ...)
    :
    : I need to use this in a multi-language website for storing static contents for different languages. I need it soon. Help me please !
    :
    : Thanks
    :
    :
    Hi.
    To save you can below simple code:
    [code]
    XmlTextWriter writer = new XmlTextWriter("simple.xml", null);
    writer.Formatting = Formatting.Indented;
    writer.WriteStartDocument();
    writer.WriteStartElement("language");

    writer.WriteAttributeString("id", "1");

    writer.WriteStartElement("key1");
    writer.WriteString("your Value1");
    writer.WriteEndElement();

    writer.WriteStartElement("key2");
    writer.WriteString("your Value1");
    writer.WriteEndElement();

    writer.WriteStartElement("key1");
    writer.WriteString("your Value3");
    writer.WriteEndElement();

    writer.WriteEndElement(); //end language tag

    [/code]

    For reading like this code but using XmlTextReader .
    With the best wishes.
    Bye.
  • mumpizmumpiz Posts: 75Member
    : : I need to store some Key/Value pairs associated with a value(id) in a XML file.The xml file would have the following structure :
    : :
    : :
    : : value
    : : value
    : : value
    : : .
    : : .
    : : .
    : :
    : :
    : :
    : : value
    : : value
    : : value
    : : .
    : : .
    : : .
    : :
    : : .
    : : .
    : : .
    : : My question is How I can save and retrieve information to and from the document.Show me the full code please.
    : : Because I may have more than one xml file of this type that each store different key/value pairs,I need a class that encapsulate everything.I mean it takes as an argument the file name and language "id" and then retrieve all key/value pairs associated with that "id" and we can access them using the class properties.Show me the attributes,methods... the class should have,the data types the class should use.(e.g. IEnumerable ...)
    : :
    : : I need to use this in a multi-language website for storing static contents for different languages. I need it soon. Help me please !
    : :
    : : Thanks


    Hi,

    obviously you are trying to create a key-value pair with your xml file. that is easily achieved by creating a *.resx file with visual studio. It will automatically create an approprieate header and an empty body, designed for key-value pairs. In VS You can choose between data and xml-view of that file and edit file in either view. To be multi-lingual You only need to create a number of different resx files, each of those representing one specific language. The key values stay same, only the values change. So depending on which language is current You load the matching files, code and key stay same. No switching and if else.

    short sample of resx:


    Abschneiden/Anschmiegen mit Blattung


    Ausrissfrei setzen


    Maschineneinheiten mit Ausrissfrei versehen.


    Dachfenster einfgen



    Within code You need to create a resourceManager and intitialize that thing with the desired resx-file, eg.:

    public class MyResStringManager
    {
    private ResourceManager mResMan = null;

    public MyResStringManager()
    {
    // BabylonStrings
    mResMan =new ResourceManager("BeaverLists.BabylonStrings", this.GetType().Assembly);
    }


    to read from that file Youll never have to do any file operation any more:

    ///
    /// find and return value for key
    ///
    /// key for babylon-string!
    ///
    public string FindByKey( string key )
    {
    try
    {
    /// using private resourceManager
    string retString = mResMan.GetString( key );
    if( retString != null && retString.Length > 0 )
    return retString;
    else
    {
    #if DEBUG
    ///
    System.Windows.Forms.MessageBox.Show( "No Value for Key! Key: " + key );
    #endif
    return key;
    }
    }
    catch(Exception ex)
    {
    .........
    return "";
    }
    }

    this is how I use localizable stuff all of the time, the various resx files are translated by professional translaters.

    greets c.
Sign In or Register to comment.