コード例 #1
0
        public XElement AddXsElementIfNotPresent(XElement parentXsElement, XElement childXsElement)
        {
            XElement parentChoiceOrSequenceElement = XsMetaModel.ChoiceOrSequenceFor(XsMetaModel.ComplexTypeFor(parentXsElement));

            if (parentChoiceOrSequenceElement == null)
            {
                throw new ArgumentException(Log.LogAndReturn("Unable to locate complexType/sequence or complexType/choice under supplied parent XSD element"));
            }

            XAttribute childXsElementAttr = childXsElement.Attribute("name");
            string     localName          = childXsElementAttr.Value;

            XNamespace ns = "*";

            IEnumerable <XElement> existingElements = parentChoiceOrSequenceElement.Descendants(ns + childXsElement.Name.LocalName);

            foreach (XElement xsElement in existingElements)
            {
                XAttribute attr = xsElement.Attribute("name");
                if (attr != null && attr.Value.Equals(localName))
                {
                    return(xsElement);
                }
            }

            parentChoiceOrSequenceElement.Add(childXsElement);
            return(childXsElement);
        }