C# 如何以编程方式添加和填充<;xs:注释>;及<;xs:documenation>;C中的标记#

C# 如何以编程方式添加和填充<;xs:注释>;及<;xs:documenation>;C中的标记#,c#,xml,xsd,C#,Xml,Xsd,我需要向现有的XML模式添加一些标记,并将其写入文件。 但是,我需要向每个现有模式添加标记。用C#实现它的最佳方法是什么 谢谢 我有以下解析模式的代码: /// <summary> /// Iterates over all elements in a XmlSchema and prints them out /// </summary> /// <param name="schema"></param>

我需要向现有的XML模式添加一些标记,并将其写入文件。 但是,我需要向每个现有模式添加标记。用C#实现它的最佳方法是什么

谢谢

我有以下解析模式的代码:

        /// <summary>
    /// Iterates over all elements in a XmlSchema and prints them out
    /// </summary>
    /// <param name="schema"></param>
    private void IterateOverSchemaElements(XmlSchema schema)
    {
        XmlSchemaComplexType complex;
        foreach (XmlSchemaObject schemaObj in schema.Items)
        {
            complex = schemaObj as XmlSchemaComplexType;
            if (complex != null)
            {
                if (OutputTextDelegate != null)
                {
                    OutputTextDelegate(string.Format("ComplexType: {0}", complex.Name));
                }
                complex.Annotation = new XmlSchemaAnnotation();                    
                //Get sequence of the complextype:
                XmlSchemaSequence sequence = complex.ContentTypeParticle as XmlSchemaSequence;
                if (sequence != null)
                {
                    // Iterate over each XmlSchemaElement in the Items collection.
                    foreach (XmlSchemaElement childElement in sequence.Items)
                    {
                        if (OutputTextDelegate != null)
                        {
                            OutputTextDelegate(string.Format("--Element: {0}", childElement.Name));
                        }                            
                    }
                }
            }
        }
    }
//
///迭代XmlSchema中的所有元素并将其打印出来
/// 
/// 
私有void IterateOverSchemaElements(XmlSchema)
{
XmlSchemaComplexType复合;
foreach(schema.Items中的XmlSchemaObject schemaObj)
{
complex=schemaObj作为XmlSchemaComplexType;
if(复杂!=null)
{
if(OutputTextDelegate!=null)
{
OutputExtDelegate(string.Format(“ComplexType:{0}”,complex.Name));
}
complex.Annotation=新的XmlSchemaAnnotation();
//获取complextype的序列:
XmlSchemaSequence序列=complex.ContentTypeParticle作为XmlSchemaSequence;
if(序列!=null)
{
//迭代Items集合中的每个XmlSchemaElement。
foreach(序列中的XmlSchemaElement子元素.Items)
{
if(OutputTextDelegate!=null)
{
OutputTextDelegate(string.Format(“--Element:{0}”,childElement.Name));
}                            
}
}
}
}
}

最好将模式作为XML文档而不是模式进行操作。例如,此代码在作为复杂类型序列一部分的每个元素定义下创建注释:

const string uri = "http://www.w3.org/2001/XMLSchema";
XmlDocument d = new XmlDocument();
d.Load(path);
XmlNamespaceManager ns = new XmlNamespaceManager(d.NameTable);
ns.AddNamespace("xs", uri);

foreach (XmlElement ct in d.SelectNodes("//xs:complexType", ns))
{
    foreach (XmlElement e in ct.SelectNodes("xs:sequence/xs:element", ns))
    {
        XmlElement a = d.CreateElement("xs", "annotation", uri);
        a.InnerText = String.Format(
            "Complex type: {0}; Element name: {1}",
            ct.GetAttribute("name"),
            e.GetAttribute("name"));
        e.AppendChild(a);
    }
}