相关疑难解决方法(0)

XElement添加了一个xmlns

我正在使用Linq to XML来创建一个新的XML文件.我从现有的XML文件中获取文件的某些部分.我使用以下代码.

var v2 = new XDocument(
  new XDeclaration("1.0", "utf-16", ""),
  new XComment(string.Format("Converted from version 1. Date: {0}", DateTime.Now)),
  new XElement(ns + "keyem",
    new XAttribute(XNamespace.Xmlns + "xsd", xsd.NamespaceName),
    new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
    new XAttribute(xsi + "schemaLocation", schemaLocation.NamespaceName),
    new XAttribute("version", "2"),
    new XAttribute("description", description),
    new XElement(ns + "layout",
      new XAttribute("type", type),
      new XAttribute("height", height),
      new XAttribute("width", width),
      settings.Root)       // XML from an existing file
Run Code Online (Sandbox Code Playgroud)

问题是它添加了xmlns =""现有文件中的第一个元素.

结果是:

<?xml version="1.0" encoding="utf-16"?>
<foo 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://tempuri.org/KeyEmFileSchema.xsd KeyEmFileSchema.xsd"
  xmlns="http://tempuri.org/KeyEmFileSchema.xsd">
  <settings xmlns="">
      ... …
Run Code Online (Sandbox Code Playgroud)

c# xml linq linq-to-xml xml-namespaces

13
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×1

linq ×1

linq-to-xml ×1

xml ×1

xml-namespaces ×1