Click here to Skip to main content
15,915,082 members
Home / Discussions / C#
   

C#

 
QuestionCannot save changes to code programatically Pin
rastaVnuce2-Sep-09 23:56
rastaVnuce2-Sep-09 23:56 
AnswerRe: Cannot save changes to code programatically Pin
stancrm3-Sep-09 0:55
stancrm3-Sep-09 0:55 
GeneralRe: Cannot save changes to code programatically Pin
rastaVnuce3-Sep-09 0:59
rastaVnuce3-Sep-09 0:59 
GeneralRe: Cannot save changes to code programatically Pin
Richard MacCutchan3-Sep-09 2:37
mveRichard MacCutchan3-Sep-09 2:37 
Questionpass multiplae parameter in crystal report Pin
mjawadkhatri2-Sep-09 23:20
mjawadkhatri2-Sep-09 23:20 
AnswerRe: pass multiplae parameter in crystal report Pin
Calla3-Sep-09 0:44
Calla3-Sep-09 0:44 
GeneralRe: pass multiplae parameter in crystal report Pin
OriginalGriff3-Sep-09 1:06
mveOriginalGriff3-Sep-09 1:06 
QuestionXElement vs XmlWriter Question Pin
Programm3r2-Sep-09 23:03
Programm3r2-Sep-09 23:03 
Hi all,

I am trying to make use of XElement instead of XmlWriter, but I am experiencing some hickups ... Sigh | :sigh:

The XmlWriter code works perfectly .. here it is:
// write the controller element and attributes
foreach (var controller in ControllerList)
{
    w.WriteStartElement("controller");
    w.WriteAttributeString("name", controller.Value.ControllerName);
    w.WriteAttributeString("description", controller.Value.ControllerDescription);
    w.WriteAttributeString("area", controller.Value.ControllerArea);
    w.WriteAttributeString("startdate", DateTimeClass.DateTimeYearMonthDay(controller.Value.StartDate));
    w.WriteAttributeString("enddate", DateTimeClass.DateTimeYearMonthDay(controller.Value.EndDate));
    w.WriteAttributeString("updatemode", controller.Value.UpdateMode);

    // write the reader element and attributes
    foreach (var reader in controller.Value.ReaderList)
    {
        w.WriteStartElement("reader");
        w.WriteAttributeString("name", reader.Value.ReaderName);
        w.WriteAttributeString("readergroup", reader.Value.ReaderGroup);
        w.WriteAttributeString("host", reader.Value.ReaderHost);
        w.WriteAttributeString("type", reader.Value.ReaderType);
        w.WriteAttributeString("startdate", DateTimeClass.DateTimeYearMonthDay(reader.Value.StartDate));
        w.WriteAttributeString("enddate", DateTimeClass.DateTimeYearMonthDay(reader.Value.EndDate));
        w.WriteAttributeString("description", reader.Value.ReaderDescription);
        // end the reader element
        w.WriteEndElement();
    }
    // end the controller element
    w.WriteEndElement();
}

-- XmlWriter Output --
	  <building plant="Rosslyn" name="0.20/0.90.11.9/plant2/HQ" description="Plant2" X="206" Y="209" Width="67" Height="56">
		<controller name="Za12436" description="Test Controller" area="D" startdate="20090902" enddate="99981231" updatemode="D">
		  <reader name="A123" readergroup="GFT" host="Za12436" type="C" startdate="20090902" enddate="99981231" description="Test reader 1" />
		  <reader name="A345" readergroup="DRG" host="Za12436" type="C" startdate="20090902" enddate="99981231" description="Test Reader 2" />
		  <reader name="A567" readergroup="HYG" host="Za12436" type="C" startdate="20090902" enddate="99981231" description="Test redscsc 3" />
		</controller>
		<controller name="ZA324578" description="Controller 2" area="F" startdate="20090902" enddate="99981231" updatemode="D">
		  <reader name="B235" readergroup="BGYT" host="ZA324578" type="C" startdate="20090902" enddate="99981231" description="JJjksjcklsljcl" />
		  <reader name="B658" readergroup="hjkhkk" host="ZA324578" type="C" startdate="20090902" enddate="99981231" description="khkhk" />
		  <reader name="B730" readergroup="GFT" host="ZA324578" type="C" startdate="20090902" enddate="99981231" description="khkhkh" />
		</controller>
	  </building>


but the XElement code does not produce the same results .. It appends the data to the first controller node OMG | :OMG: WTF | :WTF: ... here is the code:
foreach (var controller in ControllerList)
{
    xElement.Element("plant").Element("building").Add(new XComment(String.Format("Controller: {0} Declaration", controller.Key)));
    xElement.Element("plant").Element("building").Add(new XElement("controller",
        new XAttribute("name", controller.Value.ControllerName),
        new XAttribute("description", controller.Value.ControllerDescription),
        new XAttribute("area", controller.Value.ControllerArea),
        new XAttribute("startdate", DateTimeClass.DateTimeYearMonthDay(controller.Value.StartDate)),
        new XAttribute("enddate", DateTimeClass.DateTimeYearMonthDay(controller.Value.EndDate)),
        new XAttribute("updatemode", controller.Value.UpdateMode)
        ));
    // write the reader element and attributes
    foreach (var reader in controller.Value.ReaderList)
    {
        xElement.Element("plant").Element("building").Element("controller").Add(new XElement("reader",
            new XAttribute("name", reader.Value.ReaderName),
            new XAttribute("readergroup", reader.Value.ReaderGroup),
            new XAttribute("host", reader.Value.ReaderHost),
            new XAttribute("type", reader.Value.ReaderType),
            new XAttribute("startdate", DateTimeClass.DateTimeYearMonthDay(reader.Value.StartDate)),
            new XAttribute("enddate", DateTimeClass.DateTimeYearMonthDay(reader.Value.EndDate)),
            new XAttribute("description", reader.Value.ReaderDescription)
            ));
    }
}


-- xElement Output --
	  <building plant="Rosslyn" name="0.20/0.90.11.9/plant2/HQ" description="Plant2" X="206" Y="209" Width="67" Height="56">
		<controller name="Za12436" description="Test Controller" area="D" startdate="20090902" enddate="99981231" updatemode="D">
		  <reader name="A123" readergroup="GFT" host="Za12436" type="C" startdate="20090902" enddate="99981231" description="Test reader 1" />
		  <reader name="A345" readergroup="DRG" host="Za12436" type="C" startdate="20090902" enddate="99981231" description="Test Reader 2" />
		  <reader name="A567" readergroup="HYG" host="Za12436" type="C" startdate="20090902" enddate="99981231" description="Test redscsc 3" />
		  <reader name="B235" readergroup="BGYT" host="ZA324578" type="C" startdate="20090902" enddate="99981231" description="JJjksjcklsljcl" />
		  <reader name="B658" readergroup="hjkhkk" host="ZA324578" type="C" startdate="20090902" enddate="99981231" description="khkhk" />
		  <reader name="B730" readergroup="GFT" host="ZA324578" type="C" startdate="20090902" enddate="99981231" description="khkhkh" />		  
		</controller>
		<controller name="ZA324578" description="Controller 2" area="F" startdate="20090902" enddate="99981231" updatemode="D" />
	  </building>


What am I doing wrong??

Many thanks in advance.
Kind regards,


The only programmers that are better C# programmers, are those who look like this -> Green Alien | [Alien]



Java | [Coffee] Programm3r

My Blog: ^_^

AnswerRe: XElement vs XmlWriter Question Pin
Keith Barrow2-Sep-09 23:28
professionalKeith Barrow2-Sep-09 23:28 
GeneralRe: XElement vs XmlWriter Question Pin
Programm3r2-Sep-09 23:37
Programm3r2-Sep-09 23:37 
GeneralRe: XElement vs XmlWriter Question Pin
Keith Barrow2-Sep-09 23:43
professionalKeith Barrow2-Sep-09 23:43 
GeneralRe: XElement vs XmlWriter Question Pin
Programm3r3-Sep-09 0:31
Programm3r3-Sep-09 0:31 
AnswerRe: XElement vs XmlWriter Question Pin
Programm3r3-Sep-09 1:42
Programm3r3-Sep-09 1:42 
GeneralRe: XElement vs XmlWriter Question Pin
Keith Barrow3-Sep-09 2:15
professionalKeith Barrow3-Sep-09 2:15 
GeneralRe: XElement vs XmlWriter Question Pin
Programm3r3-Sep-09 3:21
Programm3r3-Sep-09 3:21 
QuestionUpdate the XElement - Question Pin
Programm3r3-Sep-09 5:04
Programm3r3-Sep-09 5:04 
AnswerRe: XElement vs XmlWriter Question Pin
PIEBALDconsult3-Sep-09 6:30
mvePIEBALDconsult3-Sep-09 6:30 
GeneralRe: XElement vs XmlWriter Question Pin
Programm3r7-Sep-09 2:20
Programm3r7-Sep-09 2:20 
QuestionNavigating Internet Explorer Pin
Vijay Mudunuri2-Sep-09 22:50
Vijay Mudunuri2-Sep-09 22:50 
AnswerRe: Navigating Internet Explorer Pin
Jacobb Michael2-Sep-09 23:31
Jacobb Michael2-Sep-09 23:31 
GeneralRe: Navigating Internet Explorer Pin
Vijay Mudunuri3-Sep-09 2:00
Vijay Mudunuri3-Sep-09 2:00 
Questionconnectionstring Pin
sepehr_sepehr2-Sep-09 22:13
sepehr_sepehr2-Sep-09 22:13 
AnswerRe: connectionstring Pin
Mohammad Dayyan2-Sep-09 22:22
Mohammad Dayyan2-Sep-09 22:22 
QuestionAn issue in windows service ? Pin
Mohammad Dayyan2-Sep-09 21:59
Mohammad Dayyan2-Sep-09 21:59 
AnswerRe: An issue in windows service ? Pin
N a v a n e e t h3-Sep-09 0:23
N a v a n e e t h3-Sep-09 0:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.