简体   繁体   English

如何将属性添加到xml模式?

[英]How to add attribute to xml schema?

I have below in memory string where I need to check the attribute "CanToggleHidden" exists or not, if not exists I need to add this attribute to the string and if "CanToggleHidden" exists I need to set the value to "TRUE". 我在下面的内存字符串中需要检查属性“ CanToggleHidden”是否存在,如果不存在,则需要将此属性添加到字符串中,如果“ CanToggleHidden”存在,则需要将值设置为“ TRUE”。 I am able to check the "CanToggleHidden" exists or not using the below code. 我可以使用以下代码检查“ CanToggleHidden”是否存在。 Any help will be appreciated. 任何帮助将不胜感激。

TextReader objTextReader = new StringReader(fieldSchemaXml);
XmlTextReader reader = new XmlTextReader(objTextReader);
reader.Read();
if (reader.GetAttribute("CanToggleHidden") == null)
{
}

Below is the string 下面是字符串

    "<Field ID=\"{b77cdbcf-5dce-4937-85a7-9fc202705c91}\" Group=\"_Hidden\" SourceID=\"http://schemas.microsoft.com/sharepoint/v4\" Name=\"IconOverlay\" StaticName=\"IconOverlay\" DisplayName=\"IconOverlay\" Type=\"Text\" Required=\"FALSE\" AllowDeletion=\"TRUE\" Version=\"6\" Sealed=\"FALSE\" Hidden=\"TRUE\" CanToggleHidden=\"TRUE\" />" 

You can use the following, first parse the string as a proper XmlDocument. 您可以使用以下方法,首先将字符串解析为适当的XmlDocument。 Then getting the rootnode (as the string only contains a single node, if this is not the case, you need to do a doc.GetElementById() ) 然后获取rootnode(因为该字符串仅包含一个节点,如果不是这种情况,则需要执行doc.GetElementById()

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        XmlDocument doc = new XmlDocument();

        doc.LoadXml("<Field ID=\"{b77cdbcf-5dce-4937-85a7-9fc202705c91}\" Group=\"_Hidden\" SourceID=\"http://schemas.microsoft.com/sharepoint/v4\" Name=\"IconOverlay\" StaticName=\"IconOverlay\" DisplayName=\"IconOverlay\" Type=\"Text\" Required=\"FALSE\" AllowDeletion=\"TRUE\" Version=\"6\" Sealed=\"FALSE\" Hidden=\"TRUE\" CanToggleHidden=\"TRUE\" />");

        var element = doc.DocumentElement;
        if (element.Attributes["CanToggleHidden"] != null)
            element.SetAttribute("CanToggleHidden", "TRUE");

        Console.WriteLine(doc.ToString());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM