状态元素内容中的令牌 StartAttribute 将导致无效的 XML 文档

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

将属性值添加到 XMLWriter 元素时遇到问题。

尝试使用 XMLWriter in C#

使用 XMLWriter.StartAttributeString() 将属性添加到 XML 元素
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;
    }

按照下面给出的语句解决问题。

第 1 步:以 XML 标签名称开始元素。

          writer.WriteStartElement("TagName");

第 2 步:开始编写属性[一个或多个]

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

第三步:属性完成后写入XML标签值。

          writer.WriteString("value:);

最后:关闭 XML 标签

          writer.WriteEndElement();

下面是更新后的代码

    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;
    }