Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将XML元素编写为<;元素/>;而不是<;元素></元素>;如果InnerText为空_C#_Xml - Fatal编程技术网

C# 将XML元素编写为<;元素/>;而不是<;元素></元素>;如果InnerText为空

C# 将XML元素编写为<;元素/>;而不是<;元素></元素>;如果InnerText为空,c#,xml,C#,Xml,我正在编写一个c#类来编写一个XML文件,该文件需要与我们使用的现有XML的结构完全匹配,以便一些遗留系统不会混淆 当元素的InnerText值为null时,我需要xml元素的元素标记如下 <element/> 每次调用InnerText的set属性访问器时,似乎都会出现一个xmlement。此代码段适用于新的XmlElements: if(text != null) { myNode.InnerText = text; } 您想要的似乎是xmlement.IsEmpty

我正在编写一个c#类来编写一个XML文件,该文件需要与我们使用的现有XML的结构完全匹配,以便一些遗留系统不会混淆

当元素的InnerText值为null时,我需要xml元素的元素标记如下

<element/>

每次调用
InnerText
的set属性访问器时,似乎都会出现一个
xmlement
。此代码段适用于新的
XmlElement
s:

if(text != null) {
    myNode.InnerText = text;
}

您想要的似乎是xmlement.IsEmpty()

文件说:

如果要以短标记格式“
”序列化元素,则返回true;长格式“
”为false

设置此属性时,如果设置为true,则删除元素的子元素,并以短标记格式序列化元素


如果某个元素没有值,请将IsEmpty属性设置为true,这将提供所需的输出

   public static void WriteXML(string path)
    {
        // Create the xml document in memory inc. xml declaration
        XmlDocument doc = new XmlDocument();
        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

        // Create the root element
        doc.AppendChild(dec);
        XmlElement rootNode = doc.CreateElement("root");
        doc.AppendChild(rootNode);

        // Create elements at root node
        XmlElement XE_level1 = doc.CreateElement("level1");
        XE_level1.InnerText = "Text";
        rootNode.AppendChild(XE_level1);

        // Create a user data element

        string s = String.Empty;

        if (String.IsNullOrEmpty(s))
        {
            XmlElement XE_level2 = doc.CreateElement("level2");
            XE_level2.IsEmpty = true;
            XE_level1.AppendChild(XE_level2);
        }
        else
        {
            XmlElement XE_level2 = doc.CreateElement("level2");
            XE_level2.InnerText = s;
            XE_level1.AppendChild(XE_level2);
        }



        doc.Save(path);


    }

正如@Binkan Salaryman所说,没有必要显式地将
IsEmpty
设置为空。只要不需要设置
InnerText
。如果在许多地方需要这种行为,可以编写简单的扩展方法:

public static class XmlUtils
{
    public static void SetInnerText(this XmlElement xmlElement, string text)
    {
        if(text != null)
            xmlElement.InnerText = text;
    }
}
然后像这样使用它:

// text can be null here, element will still looks like <level2 />
XE_level2.SetInnerText(text); 
//此处的文本可以为null,元素仍然如下
XE_level2.SetInnerText(文本);

“WriteXML(string):并非所有代码路径都返回值“对不起”。我删掉了很多例子。真正的一个有try/catch,如果有异常则返回false这与我使用的非常相似,除了:xmlement element=doc.CreateElement(“element”);如果(!String.IsNullOrEmpty(text)){element.InnerText=text;}Node.AppendChild(element);谢谢已将@Binkan Salaryman标记为答案,但这也非常有用。谢谢,谢谢你的加入
public static class XmlUtils
{
    public static void SetInnerText(this XmlElement xmlElement, string text)
    {
        if(text != null)
            xmlElement.InnerText = text;
    }
}
// text can be null here, element will still looks like <level2 />
XE_level2.SetInnerText(text);