【问题标题】:How to Remove <xml version="1.0"> Element in C#如何在 C# 中删除 <xml version="1.0"> 元素
【发布时间】:2014-08-07 10:31:49
【问题描述】:

我在变量中的 xml 内容如下所示:

 var xml = DownloadString(@"http://192.168.1.50:8983/solr/core-live/select?q=*%3A*&wt=xslt&tr=custom.xsl");

DownloadString 是一个函数/方法

public static string DownloadString(string address) 
     {
        string text;
         using (var client = new WebClient()) 
         {
           text = client.DownloadString(address);
         }
           return text;
      }

当我在 xml 变量上调试时,xml o/p 看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<xml version="1.0">
<item>
<sku>12944</sku>
<title>test</title</item>
</xml>

我想从同一个变量中删除第二个节点(&lt;xml version="1.0"&gt;)和最后一个节点(&lt;/xml&gt;)。

然后在 xml 文件中使用这个保存内容后:

 System.IO.File.WriteAllText("test.xml", xml);

问候, 贾廷

【问题讨论】:

  • 但是它不会是 XML 吗?您要删除 XML 标记吗?
  • @DrSchizo 不删除第一个标签我想删除第二个 标签。

标签: c# .net xml xml-parsing


【解决方案1】:
XDocument xdoc = XDocument.Parse(xml);
xdoc.Declaration = null;
return xdoc;

C# creating XML output file without <?xml version="1.0" encoding="utf-8"?>

【讨论】:

    【解决方案2】:

    也许你需要在字符串中使用替换方法

                    text = text.Replace("<xml version=\"1.0\">", "");
                    text = text.Replace("</xml>", "");
    

    【讨论】:

      【解决方案3】:
          string filePath = "C:\\file.xml";
                      List<string> strList = File.ReadAllLines(filePath).ToList();
                      StringBuilder sb = new StringBuilder();
                      int ctr = 0;
                      foreach (string str in strList)
                      {
                          ctr++;
                          if (ctr == 1 || ctr == strList.Count)
                              continue;
                          sb.Append(str);
                      }
      

      【讨论】:

        【解决方案4】:

        在我的情况下,除了@user1040975 的问题解决方案之外,我还必须将 XmlWriterSettings 的 OmitXmlDeclaration 属性设置为 true,因此会出现没有我创建的 Encoding 的新声明,最后代码如下所示:

        XmlWriterSettings settings = new XmlWriterSettings()
        {
            Encoding = new UTF8Encoding(false),
            OmitXmlDeclaration = true
        };
        using (XmlWriter xmlWriter = XmlWriter.Create(convertedPath, settings))
        {
            XDocument xDoc = XDocument.Parse(innerXml);
            xDoc.Declaration = new XDeclaration("1.0",null,null);
            xDoc.Save(xmlWriter);
        }
        

        【讨论】:

          【解决方案5】:

          我在 DownloadString() 方法上使用了两个字符串 replace() 函数。

          我正在尝试这段代码,它工作正常。

          public static string DownloadString(string address) 
                  {
                              string text;
                              using (var client = new WebClient()) 
                              {
                                  text = client.DownloadString(address);
                              }
                              return text.Replace("<xml version=\"1.0\">", "").Replace("</xml>", "");
                  }
          

          【讨论】:

            猜你喜欢
            • 2015-07-15
            • 1970-01-01
            • 2013-01-25
            • 2013-10-13
            • 1970-01-01
            • 2012-03-31
            • 1970-01-01
            • 2014-03-23
            • 2023-03-30
            相关资源
            最近更新 更多