0

I'm building a small software that is supposed to generate a graphml file in order to read using yEd afterwards. It is a software that builds graphs using nodes and edges.

The typical 'node' is writtent as follows

<node id="n0">
  <data key="d6">
    <y:ShapeNode>
      <y:Geometry height="80.0" width="90.0" x="0.0" y="0.0"/>
      <y:Fill color2="#993300" hasColor="false" transparent="false"/>
      <y:BorderStyle color="#000000" type="line" width="1.0"/>
      <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="14" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="21.1513671875" horizontalTextPosition="center" iconTextGap="4" modelName="internal" modelPosition="t" textColor="#000000" verticalTextPosition="bottom" visible="true" width="43.6962890625" x="23.15185546875" xml:space="preserve" y="4.0">Boite1</y:NodeLabel>
      <y:Shape type="diamond"/>
    </y:ShapeNode>
  </data>
</node>

I'm trying to edit via C# a graphml file. The way I wanted to do this, was by print lines in a rich text box and then saving it to a file with the right extension.

Although it seems I can't print quotqtion marks inside a print, as follows. The 'WriteLog' method is my method to print to the rich text box.

WriteLog("<?xml version="1.0" encoding="UTF - 8" standalone="no"?>");

Is there maybe another way i do not think about ? I'm really not familiar with xml.

  • That's just XML with a specific namespace. Instead of writing strings, use the XML classes like XDocument or XmlSerializer to generate the XML file – Panagiotis Kanavos Aug 07 '18 at 08:26
  • Yea that's what I thought, but the software needs to write nodes one by one, not once. IIsn't XDocument just creating the xml file at once? – Gregoire Rouet Aug 07 '18 at 08:30
  • You can generate classes that can be used to serialize/deserialize an XML file from its xsd with the `svcutil.exe`, `xsd.exe` tools. – Panagiotis Kanavos Aug 07 '18 at 08:30
  • `write nodes one by one, not once` you can't write nodes in XML files one-by-one. The enclosing elements always have to be closed. The *file* has to be saved as one. – Panagiotis Kanavos Aug 07 '18 at 08:31
  • Adding elements to an XDocument though can be done one at a time. It's *easier* though if you use the generated objects. You can specify that child elements are represented by `List` instead of arrays. – Panagiotis Kanavos Aug 07 '18 at 08:32
  • Okay thanks, so the way to gowould be to fill a list with each element and then creat the XDocument ? I'm going to look into this. – Gregoire Rouet Aug 07 '18 at 08:53

1 Answers1

0

Because I really needed to add nodes one by one, I had to go through a rich text box I would then save as .graphml

I simply had to escape the double quote \

My line becomes

WriteLog("<?xml version=\"1.0\" encoding=\"UTF - 8\" standalone=\"no\"?>");

Thanks for the help Panagiotis! I'll try to look into XDocument to find a 'cleaner' way than this!