Constructing simple XML?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brett Romero

    Constructing simple XML?

    What is the best way to construct this type of XML

    <ROOT><Person PersonId=\"1000 \" /><Person PersonId=\"1001 \" /><Person
    PersonId=\"1002 \" /></ROOT>

    when I'm getting back only the three numerical values? The is occuring
    in a loop and I'm getting a variable such as PersonID, which holds the
    values. The XML needs to be a string but I'd like to see what a more
    complex type would look like (XMLdoc type?).

    Thanks,
    Brett

  • Brett Romero

    #2
    Re: Constructing simple XML?

    This pretty much covers the creation part:
    http://www.csharphelp.com/archives/archive199.html. Still not sure how
    to get the two backslashes in there.

    Brett

    Comment

    • Kai Brinkmann [MSFT]

      #3
      Re: Constructing simple XML?

      Brett, you create attribute nodes just like element nodes. Here's the code
      from the link you posted, modified to create the XML in your original post:

      XmlDocument xmldoc;
      XmlAttribute attr;
      XmlElement elem;
      XmlElement root;

      xmldoc=new XmlDocument();

      //let's add the XML declaration section
      XmlNode xmlnode=xmldoc. CreateNode(XmlN odeType.XmlDecl aration,"","");
      xmldoc.AppendCh ild(xmlnode);

      //let's add the root element
      elem=xmldoc.Cre ateElement("ROO T");
      xmldoc.AppendCh ild(elem);
      root = elem;

      //add person elements. assume you have some data structure called ListOfIds
      containing the numbers
      foreach (int personId in ListOfIds)
      {
      elem = xmldoc.CreateEl ement("Person") ;
      attr = xmldoc.CreateAt tribute("Person Id");
      attr.InnerText = personId.ToStri ng();
      elem.Attributes .Append(attr)
      root.AppendChil d(elem);
      }

      --
      Kai Brinkmann [MSFT]

      Please do not send e-mail directly to this alias. This alias is for
      newsgroup purposes only.
      This posting is provided "AS IS" with no warranties, and confers no rights.


      "Brett Romero" <account@cygen. com> wrote in message
      news:1138227988 .559390.64060@g 14g2000cwa.goog legroups.com...[color=blue]
      > This pretty much covers the creation part:
      > http://www.csharphelp.com/archives/archive199.html. Still not sure how
      > to get the two backslashes in there.
      >
      > Brett
      >[/color]


      Comment

      • Brett Romero

        #4
        Re: Constructing simple XML?

        Thanks.

        If I try to read my file, which looks like this:
        <?xml version="1.0"?>
        <ROOT>
        <Person PersonID="83615 ">
        </Person>
        </ROOT>

        I get this error:
        "The data at the root level is invalid. Line 1, position 1."

        I'm using:
        xmlDoc.LoadXml( @"C:\Documen ts and Settings\BROMER O\My
        Documents\testi ng.spec");

        I'm not sure if it is referring to the LoadXML method or the file. Any
        ideas?

        The <?xml version="1.0"?> part was generated with the XMLDeclaration
        enumerator.

        Thanks,
        Brett

        Comment

        • Mark R. Dawson

          #5
          Re: Constructing simple XML?

          Hi Brett,
          the LoadXML method assumes the string you are passing as a parameter is
          XML, not a file path. If you want to load XML from a file use the
          XmlDocument.Loa d method and pass in the file name.

          Hope that helps
          Mark Dawson




          "Brett Romero" wrote:
          [color=blue]
          > Thanks.
          >
          > If I try to read my file, which looks like this:
          > <?xml version="1.0"?>
          > <ROOT>
          > <Person PersonID="83615 ">
          > </Person>
          > </ROOT>
          >
          > I get this error:
          > "The data at the root level is invalid. Line 1, position 1."
          >
          > I'm using:
          > xmlDoc.LoadXml( @"C:\Documen ts and Settings\BROMER O\My
          > Documents\testi ng.spec");
          >
          > I'm not sure if it is referring to the LoadXML method or the file. Any
          > ideas?
          >
          > The <?xml version="1.0"?> part was generated with the XMLDeclaration
          > enumerator.
          >
          > Thanks,
          > Brett
          >
          >[/color]

          Comment

          • Anders Borum

            #6
            Re: Constructing simple XML?

            For really quick XML construction, I suggest looking at the XmlWriter class
            (and its descending implementations ).

            --
            Venlig hilsen
            Anders Borum / SphereWorks
            Microsoft Certified Professional (.NET MCP)


            Comment

            Working...