problem z użyciem XmlElement jako typu zwracanego przez meto

0

Witam, chciałbym napisać metodę, która zwróci jako wynik fragment xml-a dla rożnych typów obiektów. W tym celu postanowiłem użyć XmlElement jako obiekt zwracany przez metodę. Jednak po paru eksperymentach okazało się że nie wszystko działa jak przypuszczałem.

public XmlElement ColorToXmlElement(Color col)
        {
            XmlElement answer = new XmlElement();
            answer.Name = "Color";
            answer.SetAttribute("name", col.Name.ToString());
            answer.SetAttribute("r", col.R.ToString());
            answer.SetAttribute("g", col.G.ToString());
            answer.SetAttribute("b", col.B.ToString());
            answer.SetAttribute("known", col.IsKnownColor.ToString());

            return answer;
        }
    }

I teraz mam następujące pytanie, co zrobić żeby to zadziałało?, a jeśli nie da rady, to jakiego innego obiektu użyć by przechowywał/zwracał

<something attr="attrValue"> innertext </something>

i z łatwością można by było go potem użyć z resztą dostępnej w .NET infrastruktury do obsługi XML-a

dzięki za wszelkie sugestie

0

Nie wiem czy można inaczej stworzyć XmlElement.

public static XmlElement ColorToXmlElement(Color color)
{
    XmlDocument xmlDoc = new XmlDocument();
    XmlElement xmlEl = xmlDoc.CreateElement("Color");
    xmlEl.SetAttribute("name", color.Name);
    xmlEl.SetAttribute("r", color.R.ToString());
    xmlEl.SetAttribute("g", color.G.ToString());
    xmlEl.SetAttribute("b", color.B.ToString());
    xmlEl.SetAttribute("known", color.IsKnownColor.ToString());
    return xmlEl;
}
0

Niestety, akurat tak jak napisałeś nie da rady, bo czepia się że element nalezy do innego XmlDocument, ogólnie myślałem że obędzie się bez podawania XmlDocument w parametrach, czy jak to zasugerowałeś jako jakiś tymczasowy obiekt. Czekam na dalsze sugestie.

0
            
public static XmlElement ColorToXmlElement(Color color)
{
            XmlDocument xmlDoc = new XmlDocument();
            XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "", "yes");
            xmlDoc.PrependChild(xmlDec);
            XmlElement xmlRoot = xmlDoc.CreateElement("Colors");
            xmlDoc.AppendChild(xmlRoot);

            xmlDoc = xmlRoot.OwnerDocument;
            XmlElement xmlColor = xmlDoc.CreateElement("Color");

            xmlColor.SetAttribute("name", color.Name);
            xmlColor.SetAttribute("r", color.R.ToString());
            xmlColor.SetAttribute("g", color.G.ToString());
            xmlColor.SetAttribute("b", color.B.ToString());
            xmlColor.SetAttribute("known", color.IsKnownColor.ToString());
            xmlRoot.AppendChild(xmlColor);
            return xmlRoot;
}

XML:

<?xml version="1.0" standalone="yes"?> <colors> <color name="Black" r="0" g="0" b="0" known="True" /> </colors> I dalej: ```cpp XmlDocument xmlDoc = new XmlDocument(); xmlDoc = this.ColorToXmlElement(color).OwnerDocument; xmlDoc.Save("test.xml"); ```
0
morethanchaos napisał(a)

Niestety, akurat tak jak napisałeś nie da rady, bo czepia się że element nalezy do innego XmlDocument

Hmm... a spróbuj z metodą "ImportNode" XmlDocument.

Utworzyć XmlElement bez XmlDocument się nie da, bo faktyczne dane zawiera XmlDocument, a XmlElement jest tylko interfejsem dla nich.

1 użytkowników online, w tym zalogowanych: 0, gości: 1