XElement 仅添加前缀

发布于 2024-11-06 11:33:54 字数 707 浏览 0 评论 0原文

我有一个 XML 文件,例如:

<myPrefix:Catalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
 xmlns:sys="clr-namespace:System;assembly=mscorlib" 
 xmlns:myPrefix="clr-namespace:........">

  <myPrefix:Item Name="Item1" Mode="All" />
  <myPrefix:Item Name="Item2" Mode="Single" />

</myPrefix:Catalog>

使用 C#,我创建一个新项目,例如:

XContainer container = XElement.Parse(xml);
XElement xmlTree = 
   new XElement("Item",
      new XAttribute("Name", item.Name),
      new XAttribute("Mode", item.Mode));

如您所见,我没有添加“myPrefix”前缀。谁能告诉我我该怎么做?我不想再次声明 xmlns。谢谢,彼得

I have an XML file like:

<myPrefix:Catalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
 xmlns:sys="clr-namespace:System;assembly=mscorlib" 
 xmlns:myPrefix="clr-namespace:........">

  <myPrefix:Item Name="Item1" Mode="All" />
  <myPrefix:Item Name="Item2" Mode="Single" />

</myPrefix:Catalog>

With C# I create a new Item like:

XContainer container = XElement.Parse(xml);
XElement xmlTree = 
   new XElement("Item",
      new XAttribute("Name", item.Name),
      new XAttribute("Mode", item.Mode));

As you can see I don't add the "myPrefix" prefix. Can anyone tell me how i can do that? I don't want to declarde the xmlns again. Thanks, Peter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-11-13 11:33:54
    XElement container = XElement.Parse(xml);
    XNamespace myPrefix = container.GetNamespaceOfPrefix("myPrefix");

    XElement xmlTree = new XElement(myPrefix + "Item",     
                            new XAttribute("Name", item.Name), 
                            new XAttribute("Mode", item.Mode));

    container.Add(xmlTree);
    XElement container = XElement.Parse(xml);
    XNamespace myPrefix = container.GetNamespaceOfPrefix("myPrefix");

    XElement xmlTree = new XElement(myPrefix + "Item",     
                            new XAttribute("Name", item.Name), 
                            new XAttribute("Mode", item.Mode));

    container.Add(xmlTree);
已下线请稍等 2024-11-13 11:33:54

编辑1:
如果您还将名称空间属性添加到元素,这将强制它添加前缀。但最终您仍然会在节点中得到 xmlns 属性。 要删除它,正如 Jeff 所说,您可能需要使用 XmlWriter。

编辑 2:
要获得您想要的确切 XML,您还需要创建根元素:

编辑 3:
好的。我找到了一种无需 XmlWriter 即可获得所需内容的方法:

var xml = "<myPrefix:Catalog xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:sys=\"clr-namespace:System;assembly=mscorlib\" xmlns:myPrefix=\"clr-namespace:........\"><myPrefix:Item Name=\"Item1\" Mode=\"All\" /></myPrefix:Catalog>";

XNamespace presentation = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XNamespace xaml = "http://schemas.microsoft.com/winfx/2006/xaml";
XNamespace mscorlib = "clr-namespace:System;assembly=mscorlib";
XNamespace myPrefix = "clr-namespace:.......";

XElement container = XElement.Parse(xml);

var xmlTree = new XElement("Item",
               new XAttribute("Name", "Item2"),
               new XAttribute("Mode", "Single"));

container.Add(xmlTree);

foreach (var el in container.DescendantsAndSelf())
{
  el.Name = myPrefix.GetName(el.Name.LocalName);
  var atList = el.Attributes().ToList();
  el.Attributes().Remove();
  foreach (var at in atList)
  {
    if (el.Name.LocalName == "Catalog" && at.Name.LocalName != "xmlns")
      continue;
    el.Add(new XAttribute(at.Name.LocalName, at.Value));
  }
}

container.Add(new XAttribute(XNamespace.Xmlns + "x", xaml));
container.Add(new XAttribute(XNamespace.Xmlns + "sys", mscorlib));
container.Add(new XAttribute(XNamespace.Xmlns + "myPrefix", myPrefix));

编辑 4:
显然有一种更简单的方法...更简单...请参阅其他答案。

Edit 1:
If you add the namespace attribute aswell to the element this will force it to add the prefix. But you still end up with the xmlns attribute in the node. To remove it you'll probably, as Jeff says, need to utilize XmlWriter.

Edit 2:
To get the EXACT XML you want you need to create the root element aswell:

Edit 3:
OK. I found a way to get what you want without XmlWriter:

var xml = "<myPrefix:Catalog xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:sys=\"clr-namespace:System;assembly=mscorlib\" xmlns:myPrefix=\"clr-namespace:........\"><myPrefix:Item Name=\"Item1\" Mode=\"All\" /></myPrefix:Catalog>";

XNamespace presentation = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XNamespace xaml = "http://schemas.microsoft.com/winfx/2006/xaml";
XNamespace mscorlib = "clr-namespace:System;assembly=mscorlib";
XNamespace myPrefix = "clr-namespace:.......";

XElement container = XElement.Parse(xml);

var xmlTree = new XElement("Item",
               new XAttribute("Name", "Item2"),
               new XAttribute("Mode", "Single"));

container.Add(xmlTree);

foreach (var el in container.DescendantsAndSelf())
{
  el.Name = myPrefix.GetName(el.Name.LocalName);
  var atList = el.Attributes().ToList();
  el.Attributes().Remove();
  foreach (var at in atList)
  {
    if (el.Name.LocalName == "Catalog" && at.Name.LocalName != "xmlns")
      continue;
    el.Add(new XAttribute(at.Name.LocalName, at.Value));
  }
}

container.Add(new XAttribute(XNamespace.Xmlns + "x", xaml));
container.Add(new XAttribute(XNamespace.Xmlns + "sys", mscorlib));
container.Add(new XAttribute(XNamespace.Xmlns + "myPrefix", myPrefix));

Edit 4:
Apparently there was an easier way... a LOT easier... see the other answers.

分開簡單 2024-11-13 11:33:54

您需要在命名空间中构造任何新元素。假设您知道 XML 示例中所需的命名空间的前缀,则按如下所示执行操作:

    var xml = "<myPrefix:Catalog xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:sys=\"clr-namespace:System;assembly=mscorlib\" xmlns:myPrefix=\"clr-namespace:........\"><myPrefix:Item Name=\"Item1\" Mode=\"All\" /></myPrefix:Catalog>";

    XElement catalog = XElement.Parse(xml);

    XNamespace myP = catalog.GetNamespaceOfPrefix("myPrefix");

    catalog.Add(new XElement(myP + "Item", new XAttribute("Name", "foo"), new XAttribute("Mode", "bar")));

You need to construct any new elements in the namespace. Assuming you know the prefix of the namespace you want in the XML sample then do it as follows:

    var xml = "<myPrefix:Catalog xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:sys=\"clr-namespace:System;assembly=mscorlib\" xmlns:myPrefix=\"clr-namespace:........\"><myPrefix:Item Name=\"Item1\" Mode=\"All\" /></myPrefix:Catalog>";

    XElement catalog = XElement.Parse(xml);

    XNamespace myP = catalog.GetNamespaceOfPrefix("myPrefix");

    catalog.Add(new XElement(myP + "Item", new XAttribute("Name", "foo"), new XAttribute("Mode", "bar")));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文