制作XML标签

时间:2014-07-15 09:31:23

标签: c# asp.net xml

当我尝试将xml保存到数据库中的xml字段时,我没有得到我想要的正确格式。而不是诸如。

之类的标签
  <ClassDetails>
        <name>Priority</name>
        <displayName>Priority</displayName>
        <parent>Trouble Ticket</parent>
        <type>System.Int32</type>
        <length>256</length>
        <key>false</key>
        <required>false</required>
      </ClassDetails>

它产生以下内容是我复制上述内容的最佳方法。

<TextBox Name="test" Type="System.String" displayName="Test Control" />

//这是我传递更新方法的代码

   XElement formItem = new XElement("TextBox", new XAttribute("Name", "test"),
                  new XAttribute("Type", "System.String"),
                    new XAttribute("displayName", "Test Control"));


  portalContext.UpdateFormBuilder("Test", "form Builder" ,formItem);

// UpdateFormBuilder是

 public bool UpdateFormBuilder(string formName, string Title, System.Xml.Linq.XElement ExtraFields)

     var q = from forms in portalEntities.FormBuilders
                    where forms.Name == formName                        
                    select forms;


            List<FormBuilder> formBuilderRecords = q.ToList();
            foreach (FormBuilder formsBuilder in formBuilderRecords)
            {
                formsBuilder.Name = formName;
                formsBuilder.ExtraFields = ExtraFields.ToString();
                formsBuilder.title = Title;


            }


            return SaveChanges();

1 个答案:

答案 0 :(得分:0)

将您尝试过的代码中的XAttribute()替换为XElement(),因为您希望将值作为xml元素而不是属性,例如:

XElement formItem = new XElement("ClassDetails", 
                                    new XElement("name", "Priority"),
                                    new XElement("displayName", "Priority"),
                                    new XElement("type", "System.Int32"),
                                    ......
                                    ......
                                    //do the same way for all elements
                                 );