locked
What type of element is this? RRS feed

  • Question

  •  

    hi,

     

     im using XmlTextWriter to generate an xml file from my code... (c# code)

    i need to write this type of element <Attribute1="" Attribute2="" .../>;

     

    i tried Writting an element with no name, didn't work.

    well, is there anybody who knows how to write this type of line?

     

    thx,

    Monday, October 1, 2007 1:59 PM

Answers

  • What you show there is not XML at all so XmlWriter or XmlTextWriter will not help to write that. What you can do is write

    <Element Attribute1="" Attribute2="" />

    with

    Code Block

    using (XmlWriter xmlWriter = XmlWriter.Create("file.xml"))

    {

      xmlWriter.WriteStartElement("Element");

      xmlWriter.WriteAttributeString("Attribute1", "");

      xmlWriter.WriteAttributeString("Attribute2", "");

      xmlWriter.WriteEndElement();

    }

     

     

     

    Monday, October 1, 2007 2:07 PM

All replies

  • What you show there is not XML at all so XmlWriter or XmlTextWriter will not help to write that. What you can do is write

    <Element Attribute1="" Attribute2="" />

    with

    Code Block

    using (XmlWriter xmlWriter = XmlWriter.Create("file.xml"))

    {

      xmlWriter.WriteStartElement("Element");

      xmlWriter.WriteAttributeString("Attribute1", "");

      xmlWriter.WriteAttributeString("Attribute2", "");

      xmlWriter.WriteEndElement();

    }

     

     

     

    Monday, October 1, 2007 2:07 PM
  • first off, thx for the answare..

     

    now, i understand that point but that won't exactly solve my problem.

    im going to be a little more spesific with my situations

     

    this is the sample structure i need to generate:

     

    <?xml version="1.0" encoding="utf-8" ?>
    <FileOperations>
      <FileImport FilePath="" FileName="" Server="" DB="" UsrID="" Password="" TableName="">
        <Relations>
          <Attribute1="" Attribute2="" Attribute3="" Attribute4=""/>
          <Attribute1="" Attribute2="" Attribute3="" Attribute4=""/>

          <Attribute1="" Attribute2="" Attribute3="" Attribute4=""/>

          <Attribute1="" Attribute2="" Attribute3="" Attribute4=""/>

        </Relations>
      </FileImport>
    </FileOperations>

    obviously i getting exactly what i wanted except for the middle part.

     

    now if you've seen some thing like that before, cause i haven't, and

    you know how i could write it... please give me hand...

     

    thx,

    Monday, October 1, 2007 2:30 PM
  • As I have already pointed out, <Attribute1="" Attribute2="" Attribute3="" Attribute4=""/> is not proper XML syntax, so there is no support to create that stuff with the XML APIs in the .NET framework (or probably elsewhere).

    Monday, October 1, 2007 2:49 PM
  •  

    thank you for your time mr. Honnen i knew that somehow, eventhou im new in all this .net framework,

     i was not crazy. i've been looking up information for days trying to do this....

    the difficult part now is to explain my boss that this won't be possible Wink... thxs.

     

    Monday, October 1, 2007 3:00 PM
  • I think it is better to explain that the markup you are asked to generate is not proper XML syntax.

    If you really want to create it then XmlWriter offers the WriteRaw method http://msdn2.microsoft.com/en-us/library/System.Xml.XmlWriter.WriteRaw.aspx to generate stuff that is not proper XML syntax e.g.

      xmlWriter.WriteRaw("<Attribute1=\"\" Attribute2=\"\" />");

    However what you generate that way will simply give a parse error with any XML parser trying to process it.

    Monday, October 1, 2007 3:41 PM
  •  

    one last thing,

     

    what about getting the elements one down the other...

    i mean putting them :

     

    <Element Attribute=""...>

    <Element Attribute=""...>

     

    Instead of :

     

    <Element Attribute=""...>

      

        <Element Attribute=""...>

     

    is that possible using the TextWriter?

    Monday, October 1, 2007 5:41 PM
  • If you do

    Code Block

    xmlWriterInstance.WriteStartElement("Element");

    xmlWriterInstance,WriteAttributeString("Attribute", "");

    xmlWriterInstance.WriteEndElement();

    xmlWriterInstance.WriteStartElement("Element");

    xmlWriterInstance,WriteAttributeString("Attribute", "");

    xmlWriterInstance.WriteEndElement();

     

     

    then you write two elements as siblings. That is of course possible without any problems.

    It is even possible not to have a single root element, in that case you just need to use XmlWriterSettings with ConformanceLevel set to Fragment.

    Tuesday, October 2, 2007 11:35 AM