0

I really nead to ask you this one, many of you might think it's a simple problem but please help I worked for hours on this one and I am not a step closer to the solution. I really need this one for collage.

I have to create an xml document and this one is working fine.

Now I need to define namespaces or at least I think that is what they are.

I need to insert this into my document

  <language>
    <language id="1" tag="english"/>
    <language id="2" tag="english"/> 
  </language>

And use it like this:

<item id="1">
  <item>
    <item language="1">Periods</item>
  </item>
<item/>

My code:

XmlElement element = xmldoc.CreateElement("", "item", "1");

The problem is that insted of language I get xmlns, where can I define the namespace and how do I create <language id="1" tag="english"/> ?

my problem is that i don't know how to define <language id="1" tag="english"/> and i don't know how can i use it like this <item language="1">Periods</item>

4
  • 2
    You probably know that your example is not valid xml, do you? what is that "naziv", the item closing probably should be </item>? Where do you think a namespace could solve your problem, and what exactly is the problem? Please describe a bit more on that. Commented Jun 23, 2012 at 12:32
  • ups, i forgot to translate it. the original file is in slovene so i translate it and it should be valide because this is from the template Commented Jun 23, 2012 at 12:35
  • i have edited my answer to have the output more like the specification. please answer if that helps you. Commented Jun 23, 2012 at 13:02
  • man thank you like one milion times :) this is perfect Commented Jun 23, 2012 at 13:07

1 Answer 1

1

You could probably write a simple class, for example I use a Speech class like this one:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace StackOverflowSamples
{
    [Serializable]
    public class Speech
    {
        public Speech()
        {
            this.Items = new List<LanguageItem>();
        }

        [XmlArray]
        public List<LanguageItem> Items;
    }

    [Serializable]
    public class LanguageItem
    {
        [XmlAttribute]
        public string Language { get; set; }

        [XmlAttribute]
        public int Id { get; set; }
    }
}

Which can be simply serialized with this code:

// use built in serialization mechanism
XmlSerializer mySerializer = new XmlSerializer(typeof(Speech));
// Writing the file requires a TextWriter.
TextWriter writer = new StreamWriter("test.xml");

var speech = new Speech();
var lang1 = new LanguageItem() { Id = 1, Language = "English", };
var lang2 = new LanguageItem() { Id = 2, Language = "Slovenian", };
speech.Items.Add(lang1);
speech.Items.Add(lang2);

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
//Add an empty namespace and empty value
ns.Add("", "");

mySerializer.Serialize(writer, speech, ns);
writer.Close();

Resulting in XML like this here:

<?xml version="1.0" encoding="utf-8"?>
<Speech>
  <Items>
    <LanguageItem Language="English" Id="1" />
    <LanguageItem Language="Slovenian" Id="2" />
  </Items>
</Speech>
Sign up to request clarification or add additional context in comments.

4 Comments

thank you :) that shouold do it, except how to get rid of xmlns:xsi ?
the answer gives an example on how to achieve that. it is quite simple.
what should i do whit the mySerializer ? what kind of object is it ?
copy pasta error, many excuses, just cut it off as copying the updated example. now it should work as expected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.