使用 Web 服务保存共享点自定义 Web 部件时格式正确的 XML 错误

Posted

技术标签:

【中文标题】使用 Web 服务保存共享点自定义 Web 部件时格式正确的 XML 错误【英文标题】:Well-formed XML error when saving the sharepoint custom web part using web services 【发布时间】:2012-02-12 00:31:48 【问题描述】:

我创建了一个具有自定义集合属性(选项卡)的自定义 Web 部件。当您在编辑模式下(通过浏览器)编辑和保存自定义属性时,Web 部件可以正常工作。我想使用 Web 服务更新我的自定义 Web 部件,所以我导出了 Web 部件并获取了 xml 内容并按原样传递(这样我就可以轻松地对其进行测试),但是发生了错误

The file you imported is not valid. 
Verify that the file is a Web Part description file (*.webpart or *.dwp) and that it contains well-formed XML.

我已经使用在线 xml 验证器验证了 xml 并删除了此语句

<?xml version="1.0" encoding="utf-16"?>

因为根据 XML 验证器,这是导致 XML 格式不正确的原因,但仍然没有运气,发生了同样的错误。

有什么想法吗?我无法弄清楚 Tabs 属性上的 XML 有什么问题。

代码:

    string webPartXml = string.Empty;
    string pageUrl = currenturl;

        Guid storageKey = new Guid(webpartid);

        using (StreamReader sr = new StreamReader(@"C:\tab.txt"))
                    
            webPartXml = sr.ReadToEnd();                
        
        webPartXml = webPartXml.Replace("&lt;", "<").Replace("&gt;",">");

        try
        
            svc.SaveWebPart2(pageUrl, storageKey, webPartXml, WebpartpagesSvc.Storage.Shared, false);
        catch(Exception err)
        
            throw;
        

Tab.txt (Tab.webpart)

<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="Test.TabWebPart, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c9e6068352095bx6" />
      <importErrorMessage>Cannot import this web part.</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="ChromeType" type="chrometype">None</property>
        <property name="Height" type="unit" />
        <property name="PanelHeight" type="int">0</property>
        <property name="ShowRoundedTabs" type="bool">False</property>
        <property name="AllowZoneChange" type="bool">True</property>
        <property name="AllowEdit" type="bool">True</property>
        <property name="Tabs" type="Test.Tabs, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c9e6068352095bx6">&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;Tabs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  &lt;TabInfo&gt;
    &lt;Tab&gt;
      &lt;Title&gt;html-TESTING&lt;/Title&gt;     
    &lt;/Tab&gt;
  &lt;/TabInfo&gt;
&lt;/Tabs&gt;</property>
        <property name="CatalogIconImageUrl" type="string" />
        <property name="PanelWidth" type="int">0</property>
        <property name="AllowConnect" type="bool">True</property>
        <property name="AllowClose" type="bool">True</property>
        <property name="HelpMode" type="helpmode">Navigate</property>
        <property name="ChildTab" type="System.Collections.Specialized.NameValueCollection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934ebb9" />
        <property name="Hidden" type="bool">False</property>
        <property name="Title" type="string">Tab </property>
        <property name="ChromeState" type="chromestate">Normal</property>
        <property name="AllowHide" type="bool">True</property>
        <property name="Description" type="string">for Tab Sample.</property>
        <property name="AllowMinimize" type="bool">True</property>
        <property name="TitleUrl" type="string" />
        <property name="Width" type="unit" />
        <property name="ExportMode" type="exportmode">All</property>
        <property name="Direction" type="direction">NotSet</property>
        <property name="HelpUrl" type="string" />
        <property name="TitleIconImageUrl" type="string" />
      </properties>
    </data>
  </webPart>
</webParts>

错误信息

base System.SystemException = "Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown."
InnerText = "The file you imported is not valid. Verify that the file is a Web Part description file (*.webpart or *.dwp) and that it contains well-formed XML."
InnerXml = "<errorstring xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">The file you imported is not valid. Verify that the file is a Web Part description file (*.webpart or *.dwp) and that it contains well-formed XML.</errorstring>"

【问题讨论】:

【参考方案1】:

我认为当您尝试将 xml 恢复为原始文件时会破坏它。你真的需要这行代码吗:

webPartXml = webPartXml.Replace("&lt;", "<").Replace("&gt;",">"); 

如果你删除它会发生什么? 如果它不起作用,我建议将值包装在 CDATA-tag 中以转义 xml:

string startTabProperty = @"<property name=""Tabs"" type=""Test.Tabs, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c9e6068352095bx6"">";
string endTabProperty = "></property>"; //Note the >< in order to identify correct place
webPartXml = webPartXml.Replace("&lt;", "<").Replace("&gt;",">"); 
webPartXml = webPartXml.Replace(startTabProperty, startTabProperty + "<![CDATA[");
webPartXml = webPartXml.Replace(endTabProperty , ">" + "]]>" + "</property>");

祝你好运!

【讨论】:

【参考方案2】:

在模块的Element.xml 中写入下面的代码,并在listUrlTitleUrl 中仅更改您的列表名称

<Module>
<File>
<AllUsersWebPart WebPartZoneID="wzMain" WebPartOrder="2">
    <![CDATA[
    <webParts>
    <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
    <type name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
    <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
    </metaData>
    <data>
    <properties>
    <property name="ShowWithSampleData" type="bool">False</property>
    <property name="Default" type="string" />
    <property name="NoDefaultStyle" type="string" null="true" />
    <property name="CacheXslStorage" type="bool">True</property>
    <property name="ViewContentTypeId" type="string" />
    <property name="XmlDefinitionLink" type="string" />
    <property name="ManualRefresh" type="bool">False</property>
    <property name="ListUrl" type="string" >Lists/Employee_list</property>
    <property name="TitleUrl" type="string">/Lists/Employee_list</property>
    </properties>
    </data>
    </webPart>
    </webParts>
    ]]>
    </AllUsersWebPart>
    </File>
  </Module>

【讨论】:

以上是关于使用 Web 服务保存共享点自定义 Web 部件时格式正确的 XML 错误的主要内容,如果未能解决你的问题,请参考以下文章

使用来自 WCF 服务的数据创建自定义共享点日历​​ Web 部件

具有多个 Web 部件实例的 SharePoint Web 部件存储在同一页面上

Visual Studio 2010、SharePoint 2010、外部 Web 部件

TypeScript React onChanged:尝试在共享点中执行 Web 部件时出错

在 sharepoint 中创建可视 Web 部件连接到列表

SharePoint 2010 - 页面查看器