Beispiel #1
0
        public List <XmlModel> ProcessXml(string xmlString)
        {
            XmlDocument doc = new XmlDocument();
            var         xml =
                XElement.Parse(xmlString);

            var elementsAndIndex =
                xml
                .DescendantsAndSelf()
                .Select((node, index) => new { index = index + 1, node })
                .ToList();

            List <XmlModel> elementsWithIndexAndParentIndex =
                elementsAndIndex
                .Select(
                    elementAndIndex =>
                    new
            {
                Element       = elementAndIndex.node,
                Index         = elementAndIndex.index,
                ParentElement = elementsAndIndex.SingleOrDefault(parent => parent.node == elementAndIndex.node.Parent)
            })
                .Select(
                    elementAndIndexAndParent =>
                    new XmlModel
            {
                NodeName   = elementAndIndexAndParent.Element.Name.LocalName,
                NodeId     = elementAndIndexAndParent.Index,
                ParentId   = elementAndIndexAndParent.ParentElement == null ? 0 : elementAndIndexAndParent.ParentElement.index,
                NodeValue  = elementAndIndexAndParent.Element.HasElements == true ? null : elementAndIndexAndParent.Element.Value,
                Attributes = elementAndIndexAndParent.Element.Attributes().ToList()
            })
                .ToList();
            List <XmlModel> list = new List <XmlModel>();

            foreach (var item in elementsWithIndexAndParentIndex)
            {
                XmlModel xmodel = new XmlModel();
                xmodel.NodeId    = item.NodeId;
                xmodel.ParentId  = item.ParentId;
                xmodel.NodeName  = item.NodeName;
                xmodel.NodeValue = item.NodeValue;
                StringBuilder attrName  = new StringBuilder();
                StringBuilder attrValue = new StringBuilder();
                for (int i = 0; i < item.Attributes.Count; i++)
                {
                    attrName.Append(item.Attributes[i].Name.ToString() + ";");
                    attrValue.Append(item.Attributes[i].Value.ToString() + ";");
                }
                xmodel.AttributeName  = attrName.ToString();
                xmodel.AttributeValue = attrValue.ToString();
                list.Add(xmodel);
            }

            return(list);
        }
Beispiel #2
0
 public void GetAttributes(XmlModel model, XElement xe)
 {
     if (model.AttributeName != null && model.AttributeValue != null)
     {
         string[] AttrName  = model.AttributeName.Split(';');
         string[] AttrValue = model.AttributeName.Split(';');
         int      count     = AttrName.Count() > AttrValue.Count() ? AttrName.Count() : AttrValue.Count();
         for (int i = 0; i < count; i++)
         {
             if (AttrName[i] != "" && AttrValue[i] != "")
             {
                 xe.SetAttributeValue(AttrName[i], AttrValue[i]);
             }
         }
     }
 }