0

I have an XML:

<PolicyChangeSet schemaVersion="2.1" username="" description="">
  <Note>
    <Content></Content>
  </Note>
  <Attachments>
    <Attachment name="" contentType="">
      <Description></Description>
      <Location></Location>
    </Attachment>
  </Attachments>
</PolicyChangeSet>

I have a method that populates the XML:

public static void GeneratePayLoad(string url, string policyID)
        {
            string attachmentName = policyID.Split(new string[] { "REINSTMT" }, StringSplitOptions.None)[0] + "REINSTMT";

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(AppVars.pxCentralXMLPayloadFilePath);
            XmlNode node = xmlDoc.SelectSingleNode("/PolicyChangeSet");
            node.Attributes["username"].Value = AppVars.Username;
            node.Attributes["description"].Value = "Upload Documents";

            node = xmlDoc.SelectSingleNode("PolicyChangeSet/Note/Content");
            node.InnerText = "The attached pictures were sent by the producer.";

            node = xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachments/Attachment");
            node.Attributes["name"].Value = attachmentName;
            node.Attributes["contentType"].Value = "image/tiff";

            node = xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachments/Attachment/Description");
            node.InnerText = "Combined reinstatement picture file";

            node = xmlDoc.SelectSingleNode("/PolicyChangeSet/Attachments/Attachment/Location");
            node.InnerText = url;

            xmlDoc.Save(AppVars.pxCentralXMLPayloadFilePath);
        }

My question is, what is the optimal way to populate the XML? I feel like there is an easier way to do it. For example, I'm sure there's a way to eliminate the need to select a single node multiple times (something I'm currently doing). What do you guys recommend? What is the best way?

JJ.
  • 9,580
  • 37
  • 116
  • 189
  • 1
    First, I would stop using XmlDocument and switch to XDocument (http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx) and XElement (http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.aspx) – CaffGeek Sep 05 '12 at 15:21
  • 2
    Honestly, I'd use a tool to create an object from the xml schema, then to generate the xml I'd create an instance, populate it, and serialize it to xml rather then directly manipulate the xml. – asawyer Sep 05 '12 at 15:22
  • @asawyer is correct. The BEST approach is through serialization/desierialization. Xml should be used ONLY for data transport. You deserialize your xml document into a class, then set the values in the class, then deserialize back to xml. – CaffGeek Sep 05 '12 at 15:25
  • Define a type with the info you need and use .NET XML serialization. – lgoncalves Sep 05 '12 at 15:25
  • can you guys show me an example with serialization/desierialization? – JJ. Sep 05 '12 at 15:31
  • @Testifier http://stackoverflow.com/questions/87621/how-do-i-map-xml-to-c-sharp-objects and http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization – asawyer Sep 05 '12 at 15:31
  • thanks for all the tips guys. – JJ. Sep 05 '12 at 16:08

1 Answers1

1

Using a serialization/deserialization strategy could be an option, but sometimes you want to avoid that if you do not want to pollute your code with DTOs just for that. In this last case, you could use the XDocument-related stuff as already mentioned, like this:

var doc = XElement.Parse(
    @"<PolicyChangeSet schemaVersion='2.1' username='' description=''>
    <Note>
        <Content></Content>
    </Note>
    <Attachments>
        <Attachment name='' contentType=''>
        <Description></Description>
        <Location></Location>
        </Attachment>
    </Attachments>
    </PolicyChangeSet>");

doc.Descendants("Note")
   .Descendants("Content").First().Value = "foo";

var attachment = doc.Descendants("Attachments")
                    .Descendants("Attachment").First();

attachment.Attributes("name").First().Value = "bar";
attachment.Attributes("contentType").First().Value = "baz";

...

doc.Save(...);

You will use the Load method, instead of Parse, to load your xml from a file. It's another option.

Wasp
  • 3,395
  • 19
  • 37