XElement Add function adds xmlns=“” to the XElemen

2019-06-16 12:42发布

I have a function which generates xml for a list object:

public XDocument ToXML()
{
    foreach (var row in this)
    {
        var xml = row.ToXml();
        template.Root.Add(xml);
    }
    return template;
}

The template.ToString() reads: <RootElement xmlns="urn:testTools">

The xml reads: <Example><SubElement>testData</SubElement></Example>

After the add function has executed the template.ToString() reads: <RootElement xmlns="urn:testTools"><Example xmlns=""><SubElement>testData</SubElement></Example>

So for some reason there was an empty namespace added, how can i prevent it from doing so?

4条回答
劳资没心,怎么记你
2楼-- · 2019-06-16 13:18

Set the namespace on the Example and SubElement elements to the same as the RootElement. It is adding the xmlns="" to clear the namespace for these elements.

查看更多
欢心
3楼-- · 2019-06-16 13:19

Here is an example that outputs xml without empty namespaces. Notice the bizarre Linq-centric syntax rootNamespace + "MyElementName", which is the secret. This is the same namespace as the whole document, thus no xmlns addition is necessary. This is concatenating an XNamespace + a string, which is a "+" overload that works for Linq and that Linq knows how to deal with. (Without Linq it could be a compile error to concatenate a string and a non string type). Note this was executed against a C# project file which is a handy Xml file. Output it to a console or a richtextbox control. Then take out the "rootNamespace +" and notice the difference.

        XDocument doc = null;

        using (StreamReader streamReader =
            new StreamReader(@"myXml.csproj"))
        {
            doc = XDocument.Load(streamReader, LoadOptions.None);
        }
        XNamespace rootNamespace = doc.Root.Name.NamespaceName;

        // A search which finds the ItemGroup which has Reference 
        // elements and returns the ItemGroup XElement.
        XElement element = doc.Descendants().Where(p => p.Name.LocalName == "ItemGroup"
            && p.Descendants().First<XElement>().Name.LocalName == "Reference").First<XElement>();

        // Create a completly new element with sub elements.
        XElement referenceElement = new XElement(rootNamespace + "Reference",
            new XElement(rootNamespace + "SpecificVersion", bool.FalseString),
            new XElement(rootNamespace + "HintPath", "THIS IS A HINT PATH"));

       // Add the new element to the main doc, to the end of the Reference elements.
        element.Add(referenceElement);

        // Add an attribute after the fact for effect.
        referenceElement.SetAttributeValue("Include", "THIS IS AN INCLUDE");

        rtb.Text = doc.ToString(SaveOptions.None);
查看更多
萌系小妹纸
4楼-- · 2019-06-16 13:20

I solved it by replacing the elements with an regex. Foole's solution didn't work, because i didn't always now the exact namespace at that point in code.

So here's my dirty hack that works:

template = XDocument.Parse(new Regex("<ElementName.*>")
    .Replace(template.ToString(SaveOptions.DisableFormatting), "<ElementName>"));
查看更多
姐就是有狂的资本
5楼-- · 2019-06-16 13:31

It could be that your root needs to be closed properly:

 <RootElement xmlns="urn:testTools"> to <RootElement xmlns="urn:testTools"/> 
查看更多
登录 后发表回答