views:

74

answers:

3

I want to create something like this at run-time:

  <CWS>
    <Case name="10-040-00022">
      <CaseDetailsSet>
        <CaseDetail title="Patient name" /> 
        <CaseDetail title="Date of birth" /> 
      </CaseDetailsSet>
    </Case>
  </CWS>

so I wrote something like this ( I wish to use DOM in .NET .. not the XMLWriter,etc)

    XmlDocument doc = new XmlDocument();

    XmlElement root = doc.CreateElement("CWS");
    XmlElement singleCase = doc.CreateElement("Case");

    root.AppendChild(singleCase);

    singleCase.SetAttribute("name", "10-040-00022");

    XmlElement CaseDetailsSet = doc.CreateElement("CaseDetailsSet");
    singleCase.AppendChild(CaseDetailsSet);

    XmlElement CaseDetail = doc.CreateElement("CaseDetail");
    CaseDetailsSet.AppendChild(CaseDetail);
    CaseDetail.SetAttribute("title", "Patient Name");

please have a look at it and tell me if I am oing something wrong , regardign the code I worte to create that structure above.

much appreciated.

+1  A: 

I don't see a problem with your code. If it creates the xml you want, it should be ok. There are many different ways of creating xml documents, yours seems to be okay.

Femaref
+2  A: 

Two things:

  1. You'll need to append the root to the XmlDocument.
  2. You need to add the second CaseDetail.

    XmlDocument doc = new XmlDocument();
    
    
    XmlElement root = doc.CreateElement("CWS");
    
    
    doc.AppendChild(root); // Append the root element to the XmlDocument
    
    
    XmlElement singleCase = doc.CreateElement("Case");
    
    
    root.AppendChild(singleCase);
    
    
    singleCase.SetAttribute("name", "10-040-00022");
    
    
    XmlElement CaseDetailsSet = doc.CreateElement("CaseDetailsSet");
    singleCase.AppendChild(CaseDetailsSet);
    
    
    XmlElement CaseDetail = doc.CreateElement("CaseDetail");
    CaseDetailsSet.AppendChild(CaseDetail);
    CaseDetail.SetAttribute("title", "Patient Name");
    
    
    // add the second case detail
    XmlElement CaseDetailDateOfBirth = doc.CreateElement("CaseDetail");
    CaseDetailsSet.AppendChild(CaseDetailDateOfBirth);
    CaseDetailDateOfBirth.SetAttribute("title", "Date of birth");
    
Paul Kearney - pk
To save it to the disk, doc.Save(@"c:\case.xml");
Paul Kearney - pk
thanks Sir, I was so worried about the ROOT that if I have done it wrong.
BDotA
Just looking at this makes me a bit ill.
ChaosPandion
@Paul: Thanks, so we do not need to use XMLNode anywhere in the code? that is intresting , creating everything with XMLElement and setting the correct parent for it is doing the job... nice....
BDotA
@ChaosPandion: agreed. All I tried to do was point out the problem with the code.
Paul Kearney - pk
+2  A: 

Just thought I'd show how to do this with Linq-to-XML

XElement doc = new XElement("CWS",
    new XElement("Case",
        new XAttribute("name", "10-040-00022"),
        new XElement("CaseDetailSet",
            new XElement("CaseDetail",
                new XAttribute("title", "Patient name")),
            new XElement("CaseDetail",
                new XAttribute("title", "Date of birth")))));
juharr
thank you so very much. this helps what do I need to know about for this purpose. thank again and WOW... this LINQ is so Cool to work with.
BDotA