Results 1 to 4 of 4

Thread: [RESOLVED] Working with XML Doc

  1. #1

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Resolved [RESOLVED] Working with XML Doc

    I have an xml doc ument which looks like below
    I want to edit its content so that instead of the description list items being in xml nodes it is all one string within the prerequisites node containing html and list ul/li items.
    - <prerequisites>
    - <descriptions>
    <description>Object-oriented programming</description>
    <description>SQL</description>
    <description>C#</description>
    <description>Relational Database Concepts</description>
    <description>.Net Application Development</description>
    </descriptions>
    </prerequisites>
    I think to achieve this I need to delete the descriptions node where the parent is prerequisites and replace each description node with <li>

    I am no clear how to do this.

    Code:
                    XmlDocument d = new XmlDocument();
                                   d.Load(Outline_xml.ToString());
                                   XmlNode root = d.DocumentElement;
                    XmlNodeList projectNodes = root.SelectNodes("descendant::descriptions[description]");
                    for (int i = 0; i < projectNodes.Count; i++)
                    {
                        //if (projectNodes[i].ParentNode.Name == "descriptions")
                        //{
                            projectNodes[i].ParentNode.RemoveChild(projectNodes[i]);
                        //}
            
                    }
                                d.Save(Outline_xml.ToString());

  2. #2

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Working with XML Doc

    This seems to be working
    Code:
      XmlNode root = d.DocumentElement;
                    XmlNodeList projectNodes = root.SelectNodes("descendant::prerequisites");
                    for (int i = 0; i < projectNodes.Count; i++)
                    {
                        //if (projectNodes[i].ParentNode.Name == "descriptions")
                        //{
                        // projectNodes[i].ParentNode.ReplaceChild("",projectNodes[i]);
                        String x = " &lt; ul & gt " + projectNodes[i].InnerText + " &lt; /ul & gt ";
                        projectNodes[i].InnerText = x;
                        //}
    
                    }
    
                    projectNodes = root.SelectNodes("descendant::prerequisites[description]");
    
                    for (int i = 0; i < projectNodes.Count; i++)
                    {
                        //if (projectNodes[i].ParentNode.Name == "descriptions")
                        //{
                        // projectNodes[i].ParentNode.ReplaceChild("",projectNodes[i]);
                        String x = " &lt; ul & gt " + projectNodes[i].InnerText + " &lt; /ul & gt ";
                        projectNodes[i].InnerText = x;
                        //}
    
                    }

  3. #3

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Working with XML Doc

    My XML now looks like I think I need it, but maybe not as when I try and use it in an SSIS package as an XML source only a column called name is picked up?
    <?xml version="1.0"?>
    <xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="course_description">
    <xs:complexType>
    <xs:sequence>
    <xs:element minOccurs="0" name="course_id" type="xs:string" />
    <xs:element minOccurs="0" name="country" type="xs:string" />
    <xs:element minOccurs="0" name="title" type="xs:string" />
    <xs:element minOccurs="0" name="duration" type="xs:string" />
    <xs:element minOccurs="0" name="price" type="xs:string" />
    <xs:element minOccurs="0" name="description" type="xs:string" />
    <xs:element minOccurs="0" name="prerequisites" type="xs:string" />
    <xs:element minOccurs="0" name="objectives" type="xs:string" />
    <xs:element minOccurs="0" name="topics" type="xs:string" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>

  4. #4

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Working with XML Doc

    When I use
    Code:
      d.SelectSingleNode("course_description");
                    XmlNode nodeItem = d.CreateElement("Item");
                    d.DocumentElement.AppendChild(nodeItem);
                    nodeItem.InsertAfter(d.SelectSingleNode("course_description"), nodeItem);
    To wrap everything in an item node withing the course description,
    it only seems to insert the /item markup as below?
    Any ideas what im doing wrong?
    <?xml version="1.0" encoding="utf-8" ?>
    - <course_description xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <course_id></course_id>
    <country></country>
    <title></title>
    <duration></duration>
    <price></price>
    <description></prerequisites>
    <objectives></objectives>
    <topics></topics>
    <Item />
    </course_description>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width