Avatar billede mmbn Nybegynder
21. november 2007 - 20:28 Der er 2 kommentarer

XMLSerializer problem

Mit spørgsmål lyder således:

jeg vil gerne serialisere følgende output:
<?xml version="1.0"?>
<customStates xmlns="http://schemas.microsoft.com/09/2005/communicator/customStates" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.microsoft.com/09/2005/communicator/customStates/livecommteam/sites/main/ice/Wave%2012%20Docs/CustomActivities.xsd">
      <customState ID="1" availability="online">
              <activity LCID="1033">Working from Home</activity>
              <activity LCID="1036">Equivalent French String for - Working from Home</activity>
              <activity LCID="1030">Dansk streng</activity>
      </customState>
      <customState ID="2" availability="busy">
              <activity LCID="1033">In a Live Meeting</activity>
              <activity LCID="1036">Equivalent French String for - In a Live Meeting </activity>
              <activity LCID="1030">Dansk streng</activity>
      </customState>
      <customState ID="3" availability="busy">
              <activity LCID="1033">Working</activity>
              <activity LCID="1030">Working</activity>
              <activity LCID="1036">Equivalent French String for - Working</activity>
      </customState>
      <customState ID="4" availability="do-not-disturb">
              <activity LCID="1033">Relaxing with my family</activity>
              <activity LCID="1036">Relaxing with my family</activity>
              <activity LCID="1030">Relaxing with my family</activity>
      </customState>
</customStates>


Jeg har lavet nedstående klasse, men ved ikke hvorledes jeg kan sætte xsi:schemalocation eller hvorledes jeg skal kunne oprette flere "activity" per element i viste xml.

Håber at i kan hjælpe :)
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace UC.DemoSetup
{
    /// <summary>
    /// Contains information regarding CustomPresence
    /// </summary>
    [XmlRootAttribute("CustomStates", Namespace = "http://schemas.microsoft.com/09/2005/communicator/customStates", IsNullable = false)]
    public class CustomStates 
    {
        private CustomState customState;
        public CustomState CustomState
        {
            get { return this.customState; }
            set { this.customState = value; }
        }
       
        public CustomStates() { }

        public CustomStates(CustomState customState)
        {
            this.customState = customState;
        }
    }
    public class CustomState
    {
        private int customStateId;
        private string Avalability;
        private Activity activity;

        [XmlAttribute("ID")]
        public int CustomStateID
        {
            get{return this.customStateId;}
            set{this.customStateId = value;}
        }
       
        [XmlAttribute("availability")]
        public string Availability
        {
            get{return this.Avalability;}
            set{this.Avalability = value;}
        }

        public Activity Activity
        {
            get { return this.activity; }
            set { this.activity = value; }
        }

        public CustomState() { }

        public CustomState(int customStateId, string availability, Activity activity)
        {
            this.customStateId = customStateId;
            this.Avalability = availability;
            this.activity = activity;
        }
    }

    public class Activity
    {
        private int lcid;
        private string presenceName;

        [XmlAttribute("LCID")]
        public int LCID
        {
            get { return this.lcid; }
            set { this.lcid = value; }
        }

        [XmlText()]
        public string PresenceName
        {
            get { return this.presenceName; }
            set { this.presenceName = value; }
        }

        public Activity(){}

        public Activity(int lcid, string presenceText)
        {
            this.lcid = lcid;
            this.presenceName = presenceText;
        }
    }
}
Avatar billede mmbn Nybegynder
21. november 2007 - 20:29 #1
Der kan godt være forskellig antal "activity" per customstate, eks to i en og derefter 3 ien.
Avatar billede mmbn Nybegynder
24. november 2007 - 12:49 #2
Skriv metoden:

static public void WritePresenceXml(string fileName)
{
XmlSerializer serial = new XmlSerializer(typeof(CustomStates));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("xmlns", CreateNamespace());
TextWriter w = new StreamWriter(fileName);
CustomStates c = new CustomStates();
CustomState item = new CustomState();
item.CustomStateID = 1;
item.Availability = PresenceStates.Available.ToString();
c.CustomState = item;
Activity a = new Activity();
a.LCID = 1033;
a.PresenceName = "Test";
c.CustomState.Activity = a;
serial.Serialize(w, c, ns);
w.Close();
}

og create namespace metoden:

static private string CreateNamespace()
{
string s = @" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsichemaLocation=""http://schemas.microsoft.com/09/2005/communicator/customStates/livecommteam/sites/main/ice/Wave%2012%20Docs/CustomActivities.xsd";
return s;
//http://schemas.microsoft.com/09/2005/communicator/customStates""
}

pft
//Mikkel
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester