将包含类的字典保存到文件

时间:2019-12-14 15:59:54

标签: c# .net dictionary filewriter

你好,我还有另一个问题。首先,我有以下课程:

function useNavStickey() {

    const [menuFixed, setMenuFixed] = useState(false);
    const [overlayFixed, setOverlayFixed] = useState(false);
    const [ overlayRect, setOverlayRect] = useState(false);
    const [inputRef, setInput] = useState(null);

    const stickOverlay = () => setOverlayFixed(true)

    const stickTopMenu = () => setMenuFixed(true)

    const unStickOverlay = () => setOverlayFixed(false)

    const unStickTopMenu = () => setMenuFixed(false)

    inputRef = useRef();

    useEffect(() => {
      const handleOverlayRef = (c) => {
        if (!overlayRect) {
          setOverlayRect({ overlayRect: _.pick(inputRef.current.getBoundingClientRect(), 'height', 'width') })
        }
      }

      handleOverlayRef();
    }, []);

}

 {
        [DataMember]
[XmlElement(ElementName = "RecipeBook")]
public Dictionary<string, Recipe> DRezeptbuch = new Dictionary<string, Recipe>(); 
}

    public class Recipe
{
    [XmlElement(ElementName = "Name")]
    public string Name { get; set; }

    [XmlElement(ElementName = "Size")]
    public int? Size { get; set; }

    [XmlElement(ElementName = "Ingredients")]
    public List<Ingredient> Ingredients { get; set; }
}

因此,我正在填充“ DRezeptbuch”,并希望将其写入文件。我尝试了xmlserialization,但失败了。

    public class Ingredient
{
    [XmlElement(ElementName = "Name")]
    public string Name { get; set; }

    [XmlElement(ElementName = "Amount")]
    public decimal amount;

    [XmlElement(ElementName = "Unit")]
    public Units.Unit unit;

    [XmlElement(ElementName = "Specification")]
    public List<Units.Specification> specification;

    [XmlElement(ElementName = "Category")]
    public Units.Category category;
}

然后我用这个DataContract尝试了一下,但是我也不会工作。

            //var serializer = new XmlSerializer(dRezeptbuch.GetType());
        //using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
        //{
        //    serializer.Serialize(ms, dRezeptbuch);
        //    ms.Position = 0;
        //    xmlDoc.Load(ms);
        //    return xmlDoc.InnerXml;
        //}

那我该怎么办? 有什么好方法可以保存此词典?

0 个答案:

没有答案