Home > Enterprise >  Token StartAttribute in state Element Content would result in an invalid XML document
Token StartAttribute in state Element Content would result in an invalid XML document

Time:03-29

Facing problem while adding the attribute value to the XMLWriter element.

Trying to add the Attribute to the XML element using the XMLWriter.StartAttributeString() using XMLWriter in C#

public bool WriteXML(XmlWriter writer, DataTable dtData, ref DataTable views, ref string error, string viewid, ref string fileName, ref string FileNameColumn, string ChildRootTagNameSecond, string skipcols)
    {
        bool flag = true;
        try
        {
            if (dtData != null && dtData.Rows.Count > 0)
            {
                foreach (DataRow dr in dtData.Rows)
                {
                    if (ChildRootTagNameSecond.Trim().Length > 0)
                        writer.WriteStartElement(ChildRootTagNameSecond);
                    foreach (DataColumn dc in dtData.Columns)
                    {

                        try
                        {
                            if (skipcols.Contains(dc.ColumnName.ToLower()))
                                continue;

                            if (dc.ColumnName.ToLower().StartsWith("attrib_"))
                            {
                                writer.WriteAttributeString(dc.ColumnName.Replace("Attrib_", ""), dr[dc.ColumnName].ToString());
                            }

                            writer.WriteElementString(dc.ColumnName, dr[dc.ColumnName].ToString());
                        }
                        catch (Exception e)
                        {
                            flag = false;
                            error = e.Message;
                        }
                    }
                    if (ChildRootTagNameSecond.Trim().Length > 0)
                        writer.WriteEndElement();
                    callView(writer, ref fileName, ref FileNameColumn, ref error, ref views, viewid, dr);
                }
            }
        }
        catch (Exception ee)
        {
            flag = false;
            error = ee.Message;
        }
        return flag;
    }

CodePudding user response:

Problem solved by following the statement as given below.

Step 1: Start the element with XML tag name.

          writer.WriteStartElement("TagName");

Step 2: Start writing the attribute(s)[One or more]

          writer.WriteAttributeString("attibuteName", "Attribute Value);

Step 3: After the attribute are finished then write the XML tag value.

          writer.WriteString("value:);

Finall: Close the XML tag

          writer.WriteEndElement();

Below is the updated code

    public bool WriteXML(XmlWriter writer, DataTable dtData, ref DataTable views, ref string error, string viewid, ref string fileName, ref string FileNameColumn, string ChildRootTagNameSecond, string skipcols)
    {
        bool flag = true, firstElement = true; 
        string value="";
        try
        {
            if (dtData != null && dtData.Rows.Count > 0)
            {
                foreach (DataRow dr in dtData.Rows)
                {
                    if (ChildRootTagNameSecond.Trim().Length > 0)
                        writer.WriteStartElement(ChildRootTagNameSecond);
                    foreach (DataColumn dc in dtData.Columns)
                    {

                        try
                        {
                            if (skipcols.Contains(dc.ColumnName.ToLower()))
                                continue;
                            if (dc.ColumnName.ToLower().StartsWith("attrib_"))
                            {
                                writer.WriteAttributeString(dc.ColumnName.Replace("Attrib_", ""), dr[dc.ColumnName].ToString());
                            }
                            else
                            {
                                if (!firstElement)
                                {
                                    writer.WriteString(value);
                                    writer.WriteEndElement();
                                }
                                else
                                {
                                    firstElement = false;
                                }
                                writer.WriteStartElement(dc.ColumnName);
                                value = dr[dc.ColumnName].ToString();
                            }
                            //writer.WriteElementString(dc.ColumnName, dr[dc.ColumnName].ToString());
                        }
                        catch (Exception e)
                        {
                            flag = false;
                            error = e.Message;
                        }
                    }
                    if (!firstElement)
                    {
                        writer.WriteString(value);
                        writer.WriteEndElement();
                    }
                    if (ChildRootTagNameSecond.Trim().Length > 0)
                        writer.WriteEndElement();
                    callView(writer, ref fileName, ref FileNameColumn, ref error, ref views, viewid, dr);
                }
            }
        }
        catch (Exception ee)
        {
            flag = false;
            error = ee.Message;
        }
        return flag;
    }
  • Related